I created a SQLiteDatabase where I can see the items with the ‘View’ Button and clicking on it, I get the whole database into a ListView. In this ListView I would like the items to be clickable and if I click on them, I want to add a new entry to it with the same name, but another id. My code so far:
ListView lv;
ArrayList<String> todoItems = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
hornot info = new hornot(this);
info.open();
Cursor c = info.getAllTitles();
if (c.moveToFirst())
{
do{
todoItems.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2));
}while (c.moveToNext());
}
if (todoItems.size() > 0)
{
lv.setAdapter(new ArrayAdapter<String>(sqview.this,android.R.layout.simple_list_item_1, todoItems));
}
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//-/////////////////////////
}
});
info.close();
}
As you see, I put the database items into an array, then I put them into a ListView. With an upgrade button I can add items, so I think I need to do something same here.
In the dbhelper the createEntry function:
public long createEntry(String name, String hotness) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_HOTNESS, hotness);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
In the main.java I convert the EditTexts to Strings then put the values into the database:
String name = sqlName.getText().toString();
String hotness = sqlHotness.getText().toString();
hornot entry = new hornot(dbhelp.this);
entry.open();
entry.createEntry(name, hotness);
entry.close();
So what should I write for the onItemClick function?
In the
onItemClick()method you have the position parameter that indicates the position(in the adapter) of the list row that was clicked. You could then usegetItemAtPositionon theListViewto get the data from the row:I don’t know your database structure so I can’t recomend you something about unique
id.