Android Studio根据Spinner选择从SQLite数据库中选择项目
创始人
2024-08-17 14:30:08
0

要根据Spinner选择从SQLite数据库中选择项目,你需要执行以下步骤:

  1. 在Android Studio中创建一个新的项目,包括一个Spinner和一个ListView。

  2. 创建一个SQLite数据库,并在其中创建一个数据表用于存储项目信息。

  3. 创建一个自定义的适配器类,用于将数据库中的项目数据加载到Spinner中。

public class SpinnerAdapter extends ArrayAdapter {
    private Context mContext;
    private ArrayList mProjects;

    public SpinnerAdapter(Context context, ArrayList projects) {
        super(context, 0, projects);
        mContext = context;
        mProjects = projects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_spinner_item, parent, false);
        }
        TextView textView = convertView.findViewById(android.R.id.text1);
        textView.setText(mProjects.get(position));
        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        }
        TextView textView = convertView.findViewById(android.R.id.text1);
        textView.setText(mProjects.get(position));
        return convertView;
    }
}
  1. 在MainActivity中初始化Spinner和ListView,并将适配器设置给Spinner。
public class MainActivity extends AppCompatActivity {
    private Spinner mSpinner;
    private ListView mListView;
    private SQLiteDatabase mDatabase;
    private SpinnerAdapter mSpinnerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSpinner = findViewById(R.id.spinner);
        mListView = findViewById(R.id.list_view);

        mDatabase = openOrCreateDatabase("project.db", MODE_PRIVATE, null);
        mDatabase.execSQL("CREATE TABLE IF NOT EXISTS projects (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");

        ArrayList projects = new ArrayList<>();
        Cursor cursor = mDatabase.rawQuery("SELECT * FROM projects", null);
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                String projectName = cursor.getString(cursor.getColumnIndex("name"));
                projects.add(projectName);
                cursor.moveToNext();
            }
        }
        cursor.close();

        mSpinnerAdapter = new SpinnerAdapter(this, projects);
        mSpinner.setAdapter(mSpinnerAdapter);
    }
}
  1. 添加一个Spinner的选择监听器,在选择项目时从数据库中加载对应的项目详情,并将其展示在ListView中。
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView parent, View view, int position, long id) {
        String selectedProject = (String) parent.getItemAtPosition(position);
        Cursor cursor = mDatabase.rawQuery("SELECT * FROM projects WHERE name = ?", new String[]{selectedProject});
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                // 获取项目详情并展示在ListView中
                cursor.moveToNext();
            }
        }
        cursor.close();
    }

    @Override
    public void onNothingSelected(AdapterView parent) {
        // 当没有选择项目时的处理
    }
});

这样,当你选择Spinner中的项目时,就会从SQLite数据库中加载对应的项目详情并展示在ListView中。请根据你的具体需求进行相应的修改和完善。

相关内容

热门资讯

aapoker辅助!gover... 您好,aapoker辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家...
智星德州菠萝有挂吗!德扑之星怎... 智星德州菠萝有挂吗!德扑之星怎么在电脑上玩(透视)从前有挂(详细辅助2025新版教程);智星德州菠萝...
wpk真的有外挂!德州ai软件... wpk真的有外挂!德州ai软件使用(透视)本然是有挂(详细辅助AI教程)1)wpk真的有外挂辅助挂:...
微扑克ai辅助器苹果版!fis... 微扑克ai辅助器苹果版!fishpoker app辅助(透视)原生存在有挂(详细辅助解密教程)1、许...
德州之星有辅助挂!wepoke... 德州之星有辅助挂!wepoke软件透明挂检测(透视)素来真的是有挂(详细辅助安装教程);小薇(透视辅...
wepoke有挂!云扑克辅助挂... wepoke有挂!云扑克辅助挂(透视)一贯真的有挂(详细辅助2025新版)是一款可以让一直输的玩家,...
德州ai辅助软件!德州ai在哪... 德州ai辅助软件!德州ai在哪里找(透视)原生真的有挂(详细辅助AI教程)1、这是跨平台的德州ai辅...
wpk提高胜率!fishpok... wpk提高胜率!fishpoker正规吗(透视)素来是真的有挂(详细辅助2025版教程)1、超多福利...
wepoke真的有挂!wopo... wepoke真的有挂!wopoker轻量版外挂(透视)都是是真的有挂(详细辅助2025教程)您好,w...
wepokeai代打!poke... wepokeai代打!pokernow怎么加注(透视)好像真的有挂(详细辅助靠谱教程)1.wepok...