繁体   English   中英

在Android中的列表视图中获取所选项目的空间内容

[英]Get spacial content of selected item in listview in android

我是android编程的新手,列表视图有问题在我的应用程序中,我必须从数据库中读取数据(名称,ID,年份),然后在该用户必须选择其中一项之后,将其添加到列表视图中再次活动,这时我从db中读取数据,并根据用户的选择列出其他一些项目Ol在我的第一个活动中,我读取数据并将其添加到listview中。要选择,我必须定义一个侦听器。 我像这样的代码定义

enter code here @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_read_book);

    String SDcardPath = Environment.getExternalStorageDirectory().getPath();
    String DbPath = SDcardPath + "/Tosca/" + "persian_poem.db";
    ListView list = (ListView) findViewById(R.id.list_poet_name);

    try {
        db = SQLiteDatabase.openDatabase(DbPath,null,SQLiteDatabase.CREATE_IF_NECESSARY);
        getData();
        db.close();

    }
    catch (SQLiteException e) {
        Toast.makeText(this, e.getMessage(), 1).show();
    }

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub
            ListView list = (ListView) findViewById(R.id.list_poet_name);
            Log.i(TAG, "Listview get Item Pos");
            Peot_ID.putString ("Peot_ID", (String) list.getItemAtPosition(position));
            Intent Book_list_intent = new Intent (Read.this,Book_list.class);
            Book_list_intent.putExtras(Peot_ID);
            startActivity(Book_list_intent);
        }

    });





}
private void getData() {


        try {
        //txtMsg.append("\n");
        // obtain a list of from DB
            String TABLE_NAME = "classicpoems__poet_contents";
            String COLUMN_ID = "poet_id";
            String _ID = "_id";
            String COLUMN_NAME = "poet_name";
            String COLUMN_CENTURY = "century_start";
            String [] columns ={_ID,COLUMN_ID,COLUMN_NAME,COLUMN_CENTURY};

        Cursor c = db.query(TABLE_NAME,columns,null, null, null, null, COLUMN_ID);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c, 
               new String[] {COLUMN_NAME,COLUMN_CENTURY}, new int[] {android.R.id.text1,android.R.id.text2}, 0);

    ListView list = (ListView) findViewById(R.id.list_poet_name);
    list.setAdapter(adapter);



        } catch (Exception e) {
        Toast.makeText(this, e.getMessage(), 1).show();
        }


}

但是这里我有一个问题。.我想将peot_id的数据(它从db中的_id列发送出去)发送到下一个活动。.但是我提到,使用此代码,我可以获得整行选定项,而我只想它(peot_id)您能帮我如何从所选列表项中获取Peot_ID吗? 我还有另一个问题。正如您在我的代码中看到的,我必须多次引用一个spasial列表视图。每次我通过此代码定义它

enter code hereListView list = (ListView) findViewById(R.id.list_poet_name);

我该如何一次定义此listviwe并在我的代码中的多个地方使用它?像公共变量一样,还是像那样……感谢您的帮助。

您可以像这样查询以仅获取特定列记录:

Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
              KEY_NAME, KEY_DESIGNATION}, KEY_ROWID + "=" + yourPrimaryKey, null,
              null, null, null, null);
        if (mCursor != null) {
          mCursor.moveToFirst();
        }

如您在我的代码中看到的,我必须多次引用一个spasial列表视图。每次我通过此代码定义它

只需创建一个全局ListView变量列表,然后您就可以从Activity中的任何位置访问它。 无需在OnItemClick()方法中再次声明和初始化ListView。

我想将peot_id的数据(它从db的_id列中发送出去)发送到下一个活动。.Bt我提到,使用此代码,我可以获取整行所选项目,而我只想要其中的一部分(peot_id)可以帮我如何只从选定的列表项中获取Peot_ID?

您正在使用Android定义的基本布局

android.R.layout.simple_list_item_2

我建议您为行创建自己的XML文件,然后仅从ListView获取整个View,而从View中只能获取ID

例:

listrow.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@drawable/addresses_list_selector"
    >

    <TextView 
        android:id="@+id/id_column"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView 
        android:id="@+id/name_column"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/id_column"
        />

    <TextView 
        android:id="@+id/century_column"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/name_column"
        />

</RelativeLayout>

然后与CursorAdapter使用:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listrow, c, 
      new String[] {COLUMN_ID, COLUMN_NAME, COLUMN_CENTURY}, 
      new int[] {R.id.id_column, R.id.name_column, R.id.century_column}, 0);

然后从行获取ID:

public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
   TextView id = (TextView) v.findViewById(R.id.id_column);
   if (id != null) {
      String idString = id.getText().toString();
   }
}

注意:

如果仍然要使用android的预定义布局,则需要从ID_COLUMN传递到String [],然后通过row.findViewById(<id>);从行访问ID row.findViewById(<id>);

String[] from = {ID_COLUMN, NAME_COLUMN};
int[] to = {android.R.id.text1, android.R.id.text2};
TextView id = v.findViewById(android.R.id.android.R.id.text1);
String idString = id.getText().toString();

我个人更喜欢这样使用onListItemclick()方法

//不要忘记覆盖-非常重要

@Override

public void onListItemClick(ListView l, View v, int position, long id) {
 //TODO what you what you have here the vars position - position of the selected item in list
// and also the id so you can easy trace what selection done the user
// you can play with this 

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM