android - 将单击的项目显示到由 SQLite 填充的列表视图

android - Displaying the item clicked to a Listview populated by SQLite

大家好!

我是 android 编程新手,苦苦思索如何将数据库的 _id 附加到列表视图适配器。它几乎消耗了整个试图弄清楚如何去做。


这是列表的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.activity_dictionarylist);

    filterText = (EditText)findViewById(R.id.txtsearch);
    ListView itemList = (ListView)findViewById(R.id.listView);
    DBHelper dbHelper = new DBHelper(Dictionarylist.this);
    String[] terms = dbHelper.dictionaryWords();


    listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,terms);
    itemList.setAdapter(listAdapter);
    itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Dictionarylist.this, DictionaryActvity.class);

            intent.putExtra("DICTIONARY_ID", position);

            startActivity(intent);
        }
    });

    filterText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Dictionarylist.this.listAdapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
    }

这是数据库助手的代码:

public class DBHelper extends DBCon {
public DBHelper(Context context){
    super(context);
}

public String[] dictionaryWords(){
    String query = "Select * from main_dictionary";
    Cursor cursor = this.getdbconnection().rawQuery(query, null);
    ArrayList<String> wordterms = new ArrayList<String>();
    if(cursor.moveToFirst()){
        do{
            String word = cursor.getString(cursor.getColumnIndexOrThrow("main_word"));
            wordterms.add(word);
        }
        while (cursor.moveToNext());
    }
    cursor.close();
    String[] dictionaryWords = new String[wordterms.size()];
    dictionaryWords = wordterms.toArray(dictionaryWords);
    return dictionaryWords;
}

public Quizzer getQuizById(int quizId){
    Quizzer quizzer = null;
    String query = "select * from main_dictionary where _id =" + quizId;
    Cursor cursor = this.getdbconnection().rawQuery(query, null);
    if(cursor.moveToFirst()){
        do{
            String word = cursor.getString(cursor.getColumnIndexOrThrow("main_word"));
            String tagdef = cursor.getString(cursor.getColumnIndexOrThrow("tag_definition"));
            String engdef = cursor.getString(cursor.getColumnIndexOrThrow("eng_defintion"));
            String sample = cursor.getString(cursor.getColumnIndexOrThrow("example"));
            quizzer = new Quizzer(word, tagdef, engdef, sample);
        }
        while(cursor.moveToNext());

    }
    cursor.close();
    return quizzer;
}

如有任何帮助,我将不胜感激。

AFAIK 在 RecyclerView 的 onBindViewHolder(ViewHolder holder, int position) 中,您可能想要附加 holder.setTag(model.getId) 而不是我们所做的 holder.setTag(position)。通过 holder.setOnClickListener(exampleListener)OnClickListener 移到外面,然后使用 int positionId = (int) holder.getTag() 将标签放入 View.OnclickListener 中。这样你也可以使用 RecyclerView。