Tuesday, April 24, 2012

Android listview with multiple controls

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.





No comments:

Post a Comment