Android database to listview clicklistener - android

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

Related

Android Studio getting value out of listview

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

Android getting data out of listview

I'm at the moment programming an APP with an listview which is filled with data from an SQLite Table like this:
public void printDatabase(){
Cursor c = handler.getAllRows();
String[] fieldNames = new String[] {DBHandler.COLUMN_WORKER_ID, DBHandler.COLUMN_WORKER_NAME, DBHandler.COLUMN_WORKER_SURNAME, DBHandler.COLUMN_WORKER_COST};
int[] toView = new int[] {R.id.item_worker_id, R.id.item_worker_name, R.id.item_worker_surname, R.id.item_worker_cost};
SimpleCursorAdapter cAdapter;
cAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.worker_items, c, fieldNames, toView, 0);
list = (ListView) findViewById(R.id.worker_listView);
list.setAdapter(cAdapter);
}
Now I'd like to get the data from the listview of an item by clicking the item. I've searched everywhere to find a solution for this and found this:
class ItemListener implements AdapterView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Object o = list.getItemAtPosition(i);
String s = list.toString();
}
}
But all I get from this is the reference to the cursor I've used for filling the listview. What do I need to get the data from the listview?
You are half way to your solution already.
Cursor itemCursor = (Cursor) list.getItemAtPosition(i);
This returns you a cursor pointing to the row that is clicked. You can get the data out of it like:
class ItemListener implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Cursor itemCursor = (Cursor) list.getItemAtPosition(i);
String workerId = itemCursor.getString(itemCursor.getColumnIndex(DBHandler. COLUMN_WORKER_ID));
String workerName = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.COLUMN_WORKER_NAME));
String workerSurname = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.COLUMN_WORKER_SURNAME));
String workerCost = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.COLUMN_WORKER_COST));
}
}
Make the cursor global ;
Cursor cursor ;
In your OnItemClick
class ItemListener implements AdapterView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
cursor.moveToPosition(position);
String Id = cursor.getString(itemCursor.getColumnIndex(DBHandler. COLUMN_WORKER_ID));
}
}
Try this:
class ItemListener implements AdapterView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView = (TextView) view.findViewById(R.id.list_content);
String text = textView.getText().toString();
//Log.d("Choosen: " + text);
}}

How get item from a listView Android?

How can I get a item from a listView 2 items , I am using to fill the listView a database of queries but there do not know how to do that to get the name of the item ?
from = new String[]{manager.CN_NAME,manager.CN_CREDITS};
to = new int[]{android.R.id.text1,android.R.id.text2};
manager = new DataBaseManager(this);
listaMateria = (ListView) findViewById(R.id.listView);
cursor = manager.cargarCursorMateria();
adapter = new SimpleCursorAdapter(this,android.R.layout.two_line_list_item,cursor,from,to,0);
listaMateria.setAdapter(adapter);
listaMateria.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String item = ((TextView)view).getText().toString();
String a = Integer.toString(to[position]);
Toast.makeText(getBaseContext(), a, Toast.LENGTH_LONG).show();
}
});
You could also get the cursor for that row.
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Cursor cursor = (Cursor)adapter.getItem(position);
String value = cursor.getString(cursor.getColumnIndex("column_name"))
}

Trying to get String from ListView

My current problem is I have a ListView I'm using to display my contacts now I'd like to set the onItemClick to grab the name of the contact and set that to a TextView called p2 the code I currently have is:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.setp2);
cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
String[] from = new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
int[] to = new int[]{R.id.id_text,R.id.name_text};
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(setp2.this,R.layout.setp2_layout,cursor,from,to);
ListView list = (ListView)findViewById(R.id.contact_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
}
So basically I'm unsure of what I need to do in the onItemClick, any help appreciated!
In your onItemClick, you will get the instance of the view associated with that list item.From that view you can get the text associated with it and set it to the p2 TextView. If my understanding of your requirement is correct, the code will be something like this:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CharSequence name = ((TextView)view.findViewById(R.id.name_text)).getText();
//Set this to your textview in P2
tvP2.setText(name);
}
});
Set up a SimpleCursorAdapter.CursorToStringConverter and set it on the adapter, then use convertToString(Cursor cursor) to get the text in on item click :
(Cursor) simple.getItem(position)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
cursor.moveToPosition(position);
String name = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
p2.setText(name);
}
});

How I can use onListItem to start a new activity with the ListView Value?

Hi I have a android application and i use a sqlite database and a listview in my Activity.
Now I want to use onListItemClick but I don_t know how I can get the value that I click and open a new activity with this value :(
here my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
mHelper = new DatenbankManager(this);
mDatenbank = mHelper.getReadableDatabase();
ladeDaten();
}
my ladeDaten method:
private void ladeDaten() {
Cursor KlassenCursor = mDatenbank.rawQuery(KLASSEN_SELECT_ROW, null);
startManagingCursor(KlassenCursor);
android.widget.SimpleCursorAdapter KlassenAdapter = new android.widget.SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
KlassenCursor,
new String[] {"name"},
new int[] {
android.R.id.text1
});
setListAdapter(KlassenAdapter);
}
Here my onListItemClick that don't work :(
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}
I think you using cursor to get data from the database.Instead of using onListItemClick() use onItemClickListener() To get the listView item details use the following code
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String itemid = cursor.getString(cursor.getColumnIndex("ColumnName");//Repeat for other values
//Start the activity here
Intent todayreview = new Intent(ReviewPayment.this,
ReviewandResend.class);
todayreview.putExtra("iteid", itemid);
startActivity(todayreview);
}
});
To use onListItemClick Add extends ListActivity.
public class MainActivity extends ListActivity
But i think it is better to use onItemClick() like Vino said

Categories

Resources