使用CursorAdapter在Activity上设置ClickListener

时间:2018-06-08 22:20:30

标签: android

编辑: 我解决了它:

Data: undefined Error undefined

----------------------------------------------- -------------------------------------------------- ------

我需要在 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); 上设置一个itemClickListener,其内容由CursorAdapter在正常ListView中设置,在我的情况下是Activity而不是AppCompatActivity。我看到了一些示例,但每个示例都是关于ArrayAdapter或Activity是ListActivity

这是ListActivity代码:

Java

当然我设置了@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_workout_list); listView = findViewById(R.id.workout_listview); try { SQLiteOpenHelper sqLiteOpenHelper = new TrainingDatabaseHelper(this); db = sqLiteOpenHelper.getReadableDatabase(); cursor = db.query("WORKOUT", new String[]{"_id", "NAME"}, null, null, null, null, "NAME ASC"); CursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, new String[]{"NAME"}, new int[]{android.R.id.text1}, 0); listView.setAdapter(cursorAdapter); } catch (SQLiteException e) { ...catching... } } listView

1 个答案:

答案 0 :(得分:0)

只需在listview上设置itemclicklistener

listView.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
         Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
    }
});