android - 显示项目单击到由SQLite填充的Listview

时间:2017-01-05 01:47:29

标签: android sqlite listview android-cursor

大家好日子!

我是android编程的新手,在如何将数据库的_id附加到listview适配器上苦苦挣扎。它几乎整体消耗,试图找出如何做到这一点。

以下是列表代码:

Window {speechSynthesis: SpeechSynthesis, caches: CacheStorage, localStorage: Storage, sessionStorage: Storage, webkitStorageInfo: DeprecatedStorageInfo…}

temp2.js:112 ciao!

temp2.js:113 undefined

以下是Database Helper的代码:

@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) {

        }
    });
    }

我将不胜感激。

1 个答案:

答案 0 :(得分:0)

在RecyclerView onBindViewHolder(ViewHolder holder, int position)中的AFAIK,您可能希望附加holder.setTag(model.getId)而不是我们所做的holder.setTag(位置)。在OnClickListener外面holder.setOnClickListener(exampleListener),在View.OnclickListener内使用int positionId = (int) holder.getTag()获取该标记。这样你也可以使用RecyclerView。