I am new to android. I am developing an application in which there is a list view of students with edit and delete buttons. Like
STUDENT LIST[ABC] [edit] [delete]
[DEF] [edit] [delete]
With this code im able to list student details in a <Textview>
public class DataBaseDemoActivity extends ListActivity {
/** Called when the activity is first created. */
SQLiteDatabase db;
Button btnInsert;
ArrayAdapter<String> students;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
db=openOrCreateDatabase("StudentDB",SQLiteDatabase.CREATE_IF_NECESSARY,null);
Cursor c=db.rawQuery("SELECT * FROM temp",null);
String[] students = new String[c.getCount()];
if (c.moveToFirst())
{
for (int i = 0; i < c.getCount(); i++)
{
students[i] = c.getString(0).toString()+" "+c.getString(1).toString();
c.moveToNext();
}
}
c.close();
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, students));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}catch(SQLException e)
{
}
}
}
I need to store the id of the record with in the list view so that when i click on the edit or delete button, i’ll have to find out the id and make changes on the DB. How can i set values to two fields say <TextView>[show details] and <EditText>[visibility: insisible – to save id of the record]. I am able to get the details to the <TextView> using the above code. Any suggestion will be appreciated.
UPDATE :
Layout i am using
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:inputType="text"
android:onClick="showInfo"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:textSize="16sp"
android:id="@+id/studentInfo" >
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/action"
android:onClick="showInfo"
/>
</LinearLayout>
You can get the Id within cursor data fetch loop, use any
HashMap<id,String>collection for it and store id along with data (text).As per your requirement, I suggest you to use
SimpleCursorAdapterinstead ofArrayAdapterwithListView.So you can manage easily your database records with your list and also get the all information related to database for particular record when List Item clicked.
Look at SimpleCursorAdapters and ListViews
Creating a custom CursorAdapter for Android