Sebastian Sebastian - 1 month ago 10
Android Question

Cant get id in custom listview

Main Activity

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);
}
});


Add class

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" />


How can i get the id of the clicked list and show those details for edit in the next page.?How can i do it. Please help me. The next page is used to edit and delete purposes

Answer

You need to use the view to find view id for list elements.
Change below line in onItemClick()

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

to

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