Android Studio从列表视图中获取价值

时间:2017-02-09 12:57:57

标签: android sqlite listview

private  void populateListView(){
    Cursor c = loginDataBaseAdapter.allData();

    startManagingCursor(c);

    String[] fromfields = new String[]{"_id", "NAME", "YEAR", "GENRE"};
    int[] toViewIDs = new int[]{R.id.TVmovieID, R.id.TVmovieTtile, R.id.TVmovieYear, R.id.TVmoviegenre};
    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, R.layout.mlayout, c, fromfields, toViewIDs);
    lv.setAdapter(myCursorAdapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String s = parent.getItemAtPosition(position).toString();
            Toast.makeText(Proba.this, "String"+s, Toast.LENGTH_SHORT).show();
        }
    });
}

这是我调用onCreate这个Activity的方法,我使用toast只是为了确保我得到正确的值,但我得到android.database.sqlite ....我想得到一个字段的值,但首先我想得到整个列表的值。

1 个答案:

答案 0 :(得分:0)

正确的实施方式来获取任何字段

final String[] fromfields = new String[]{"_id", "NAME", "YEAR", "GENRE"};
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Cursor cursor = myCursorAdapter.getItem(position);
        int columnIndex = cursor.getColumnIndexOrThrow(fromfields[1])
        String name = cursor.getString(columnIndex);
        Toast.makeText(Proba.this, "Name is " + name, Toast.LENGTH_SHORT).show();
    }
});