无法在自定义列表视图中获取ID

时间:2016-10-20 06:01:15

标签: android listview

主要活动

 public class MainActivity extends AppCompatActivity {
Button b1,b2;
DBHelper mydb;
TextView id;
ListView listView;
SimpleCursorAdapter adapter;
String[] from = new String[] { mydb.ID,
        mydb.NAME, mydb.ADDRESS };
int[] to = new int[] { R.id.id, R.id.name, R.id.address };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mydb = new DBHelper(this);





    Cursor cursor = mydb.fetch();

    listView = (ListView) findViewById(R.id.listView);


    adapter = new SimpleCursorAdapter(this, R.layout.list, cursor, from, to, 0);
    adapter.notifyDataSetChanged();

    listView.setAdapter(adapter);




    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            id=(TextView) findViewById(R.id.id);
            int id_To_Search = Integer.valueOf(id.getText().toString());

            Bundle dataBundle = new Bundle();
            dataBundle.putInt("_id", id_To_Search);

          Intent intent = new Intent(getApplicationContext(), AddPatient.class);

            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });

添加课程

  if (extras != null) {
        b2.setVisibility(View.INVISIBLE);
        b3.setVisibility(View.VISIBLE);
        b4.setVisibility(View.VISIBLE);
        int Value = extras.getInt("_id");

        if (Value > 0) {

            Cursor rs = mydb.getData(Value);
            id_To_Update = Value;
            rs.moveToFirst();



            name.setText(rs.getString(rs.getColumnIndex(DBHelper.NAME)));
            address.setText(rs.getString(rs.getColumnIndex(DBHelper.ADDRESS)));
            mobile.setText(rs.getString(rs.getColumnIndex(DBHelper.MOBILE)));
            if(rs.getString(rs.getColumnIndex(DBHelper.GENDER)) != "female")
            {
                male.setChecked(true);
            }
            else
            {
                female.setChecked(true);
            }
        }

                }

DBHelper

 public Cursor getData(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    return db.rawQuery("select * from patients where _id=" + id + "", null);
}

main.xml中

  <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="65dp" />

list.xml

  <TextView
    android:id="@+id/id"
    android:layout_width="25dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dp"
    android:padding="3dp"
    android:visibility="invisible" />

<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:padding="3dp"
    android:fontFamily="sans-serif"
    android:textSize="18sp"
    android:layout_toEndOf="@+id/id"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:id="@+id/address"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:ellipsize="end"
    android:maxLines="2"
    android:padding="3dp"
    android:visibility="visible"
    android:layout_marginTop="25dp"
    android:textSize="18sp"
    android:layout_toEndOf="@+id/id"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

如何获取所点击列表的ID并在下一页显示这些详细信息以供编辑。我怎么能这样做。请帮我。下一页用于编辑和删除目的

2 个答案:

答案 0 :(得分:1)

您需要使用视图查找列表元素的视图ID 在onItemClick()

中更改以下行
 id=(TextView) findViewById(R.id.id);

 id=(TextView) arg1.findViewById(R.id.id);

答案 1 :(得分:1)

您不应该依赖视图ID来访问数据。

你应该有一个单独的模型(让我们说一个ArrayList)包含listview中显示的数据。当用户选择一个项目时,您可以获取所列项目的位置,并在数组中选择您可以找到所有信息的相应项目