是否有可能从我的listview中获取数据

时间:2018-03-20 13:13:14

标签: android listview

您好我已经完成了几个不同的教程,从sql数据库中获取数据到listview。我可以添加数据,从数据库中获取数据并填充列表视图,并有一个工作onclick监听器(将触发Toast消息)。但是,单击时我无法从列表视图中获取任何数据。我尝试了getitem和getItemAtPosition的不同组合,但它们都返回一个空字符串(空白吐司)。有人会友好地看我的代码并告诉我,我想做的事情是否可行。在我的listview中,我在每个条目中有四个项目,我想要直接获得第四个项目或获取所有项目(作为字符串?)然后我可以提取我需要的数据。 提前感谢您的时间。

public class ListViewActivity extends Activity {

SQLiteHelper SQLITEHELPER;
SQLiteDatabase SQLITEDATABASE;
Cursor cursor;
SQLiteListAdapter ListAdapter ;

ArrayList<String> ID_ArrayList = new ArrayList<String>();
ArrayList<String> GENRE_ArrayList = new ArrayList<String>();
ArrayList<String> NAME_ArrayList = new ArrayList<String>();
ArrayList<String> URL_ArrayList = new ArrayList<String>();
ListView LISTVIEW;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    LISTVIEW = (ListView) findViewById(R.id.listView1);

    SQLITEHELPER = new SQLiteHelper(this);

}

@Override
protected void onResume() {

    ShowSQLiteDBdata() ;

    super.onResume();
}

private void ShowSQLiteDBdata() {

    SQLITEDATABASE = SQLITEHELPER.getWritableDatabase();

    cursor = SQLITEDATABASE.rawQuery("SELECT * FROM demoTable1", null);

    ID_ArrayList.clear();
    GENRE_ArrayList.clear();
    NAME_ArrayList.clear();
    URL_ArrayList.clear();

    if (cursor.moveToFirst()) {
        do {
            ID_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_ID)));

           GENRE_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_Genre)));

            NAME_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_Name)));

            URL_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_Url)));

        } while (cursor.moveToNext());
    }

    ListAdapter = new SQLiteListAdapter(ListViewActivity.this,

            ID_ArrayList,
            GENRE_ArrayList,
            NAME_ArrayList,
            URL_ArrayList

    );

    LISTVIEW.setAdapter(ListAdapter);


    LISTVIEW.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                                         long id) {
           // String text = (String) LISTVIEW.getAdapter().getItem(position);
            String text = (String) LISTVIEW.getItemAtPosition(position);
            //String text = (String) lv.getItemAtPosition(0);

           // Object item = (Object) LISTVIEW.getItemAtPosition(position);
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

        }
    });

    cursor.close();
}

}

4 个答案:

答案 0 :(得分:1)

LISTVIEW.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {

        String value1 = ID_ArrayList.get(position);

        String value2 = GENRE_ArrayList.get(position);

        String value3 = NAME_ArrayList.get(position);

        String value4 = URL_ArrayList.get(position);

        Toast.makeText(getApplicationContext(),value1+" "+value2+" "+value3+" "+value4, Toast.LENGTH_SHORT).show();
    }
});

答案 1 :(得分:0)

尝试更改行

  

String text =(String)LISTVIEW.getItemAtPosition(position);

  

String text =(String)parent.getItemAtPosition(position);

这应该是ListView的工作方式。 另外,我建议您不要将Capital Cases与变量一起使用,通常在Java中使用CamelCase约定。并且还看看RecyclerView,它通常比ListView实现得多,因为允许高水平的自定义

答案 2 :(得分:0)

试试这个, 在onCreate()中使用showSQLiteDBdata()而不是onResume()方法

答案 3 :(得分:0)

请使用listview setOnItemClickListener中的代码: -

      String genreID = ID_ArrayList.get(position);

      String genre = GENRE_ArrayList.get(position);

      String genreName = NAME_ArrayList.get(position);

      String genreUrl = URL_ArrayList.get(position);

Toast.makeText(getApplicationContext(),genreID +“,”+ genre +“,”+ genreName +“,”+ genreUrl +“,”+,Toast.LENGTH_SHORT).show();

返回列表视图的渲染数据。