private void populateListView(){
Cursor c = loginDataBaseAdapter.allData();
startManagingCursor(c);
String[] fromfields = new String[]{"_id", "NAME", "YEAR", "GENRE"};
int[] toViewIDs = new int[]{R.id.TVmovieID, R.id.TVmovieTtile, R.id.TVmovieYear, R.id.TVmoviegenre};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, R.layout.mlayout, c, fromfields, toViewIDs);
lv.setAdapter(myCursorAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String s = parent.getItemAtPosition(position).toString();
Toast.makeText(Proba.this, "String"+s, Toast.LENGTH_SHORT).show();
}
});
}
This is a method I call onCreate of this Activity, I use toast just to make sure that im getting the right value, but I get the android.database.sqlite... . I want to get the value of just one field, but first I want to get the value of the whole list.
Correct way of implementation to get any fields
final String[] fromfields = new String[]{"_id", "NAME", "YEAR", "GENRE"};
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = myCursorAdapter.getItem(position);
int columnIndex = cursor.getColumnIndexOrThrow(fromfields[1])
String name = cursor.getString(columnIndex);
Toast.makeText(Proba.this, "Name is " + name, Toast.LENGTH_SHORT).show();
}
});
Related
Trying to get name when click on the list view which connected to database.
Searched for solutions but can't find exactly like this. Trying to solve for long time by myself but failed to do. My code:
public void v() {
Cursor cursor = myDBHandler.getDatabaseCursor();
final String []arr = new String[]{myDBHandler.getColumnName()};
int idView[]=new int[]{R.id.lv_tv2};
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.lvlayout,
cursor, arr, idView);
ListView lv=(ListView)findViewById(R.id.ap_Lv1);
lv.setAdapter(simpleCursorAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor1 = myDBHandler.getDatabaseCursor();
String s = cursor1.getString(cursor1.getColumnIndex(myDBHandler.getColumnName()));
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
cursor1.close();
}
});
}
in MyDBHandler class getColumnName() method is:
public String getColumnName(){
return COLUMN_NAME;
}
mCursor.moveToPosition(position);
String s = mCursor.getString(mCursor.getColumnIndex(myDBHandler.getColumnName()));
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
i have a problem with my list view, i display an item and subitem , i want to insert the two in database but it doesn't work !! the error is that i can't cast it to a text view
please someone help me.
This is the error that i get :
java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
Here the code of List View :
String[] databaseColumnNames = new String[] { DBAdapter.col_N_Ordre,DBAdapter.col_Nom_prénom};
int[] toViewIDs = new int[] { android.R.id.text1,android.R.id.text2 };
SimpleCursorAdapter myCursordapter = new SimpleCursorAdapter(this,android.R.layout.simple_expandable_list_item_2 , cursor, databaseColumnNames, toViewIDs);
ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(myCursordapter);
Here the code of insertion :
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> arg0, View arg1,
final int position, long arg3) {
Toast.makeText(getApplicationContext(), " " + position, Toast.LENGTH_LONG).show();
final String s = ((TextView)arg1).getText().toString();
db.insertest(s);
The query is :
public long insertest(String region) {
ContentValues initialValues = new ContentValues();
initialValues.put(col_Region,region);
//initialValues.put(col_Provence_prefecture );
return db.insert(MENAGE,null, initialValues);
}
Put following code in your listview item click listener
TextView v1 = (TextView)arg1.findViewById(android.R.id.text1);
String first = v1.getText().toString();
TextView v2 = (TextView)arg1.findViewById(android.R.id.text2);
String second = v2.getText().toString();
ContentValues cv = new ContentValues();
cv.put(DBAdapter.col_Region, first);
cv.put(DBAdapter._id, second);
myCursordapter.notifyDataSetChanged();
list.setAdapter(adapter);
DBAdapter sql_Adapter = new DBAdapter(context);
sql_Adapter.open();
db.insertest(cv);
sql_Adapter.close();
I've got a functioning search interface on my app. Now I'm trying to make each search result list item clickable and display in a new activity. I've got them clickable, but I can't figure out how to pass the ListAdapter values (records) into the onItemClick method.
Here's my code so far:
private void showResults(String query) {
Cursor cursor = DBHelper.searchDB(query);
startManagingCursor(cursor);
String[] searchFrom = new String[] { DBAdapter.KEY_MAKE,
DBAdapter.KEY_YEAR, DBAdapter.KEY_MAKE,
DBAdapter.KEY_MODEL };
int[] displayHere = new int[] { R.id.rDateTV, R.id.rYearTV,
R.id.rMakeTV, R.id.rModelTV };
final SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record_2, cursor, searchFrom, displayHere);
setListAdapter(records);
DBHelper.close();
// --- Click on list item ---
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//--- here's the problem: how do I pass the ListAdapter values into onItemClick. the below doesn't like .getText()
String sYear = (view.findViewById(R.id.rYearTV)).getText().toString();
String sMake = (view.findViewById(R.id.rMakeTV)).getText().toString();
String sModel = (view.findViewById(R.id.rModelTV)).getText().toString();
// -- then pass these strings to an intent to launch a new activity
}
});
// --- END click on list item ----
}
Any ideas? I'm a rookie here, so if you could show me the code, that would be AWSOME!
In OnItemClickListener.onItemClick call parent.getItemAtPosition(position). That method returns cursor which is set to the given position. Then get your data from that cursor. Don't close cursor inside OnItemClickListener.onItemClick.
for example:
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor)parent.getItemAtPosition(position)
String sMake = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_MAKE));//get data from cursor
...
}
});
I have an Activity with ListView took in my Database. I would like to, when I select one, get the id field.
lv = (ListView) findViewById(R.id.listMessageConversationView);
Cursor c = selectInfoInDB();
int[] to = new int[] {R.id.idMessageClavier, R.id.nomMessageClavier, R.id.valeurMessageClavier, R.id.groupeMessageClavier, R.id.occurrenceMessageClavier};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.conversation_clavier_display_data, c, SmartAccess_v1Activity.nomColonnesMessage, to);
lv.setAdapter(sCA);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView)findViewById(R.id.idMessageClavier);
}
});
The fields in the ListViews are correct, but the toast give me the id of the first item of ListView, wherever I touch on list.
I worked on that and i I can't figure where is the mistake -_-
Thanks for your help, korax
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int postion,
long id) {
Toast.makeText(this, "id is :: " + id +"position :: " + position,
Toast.LENGTH_SHORT).show();
}
});
I have the following code snippet.
public class ImageStoreActivity extends ListActivity {
private DBHelper mDB;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDB = new DBHelper(this);
mDB.Reset();
Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
mDB.createItemEntry(new ListObject(img, "x", "999999", "blah"));
mDB.createItemEntry(new ListObject(img, "y", "56789", "blah blah"));
mDB.createItemEntry(new ListObject(img, "Pfirsich", "4112344", "blaflakf"));
mDB.createItemEntry(new ListObject(img, "Zitrone", "4023232", "511131"));
String[] columns = {mDB.KEY_ID, mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_PHONE, mDB.KEY_RELATION};
String table = mDB.RELATION_TABLE;
Cursor c = mDB.getHandle().query(table, columns, null, null, null, null, null);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.main,
c,
new String[] {mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_PHONE, mDB.KEY_RELATION},
new int[] {R.id.img, R.id.txt, R.id.textview,R.id.textview1});
adapter.setViewBinder(new ItemViewBinder());
setListAdapter(adapter);
}
}
how do i add this code
public void onItemClick(AdapterView parentView, View v,
int position, long id) {}
to the above code. please help me
add this code after setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// add the array list here..
}
});
Override the onListItemClick() method, like this,
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Do something here
...
}