Android: Listview's onItemClick() event not getting called - android

Following is my code :
public class TestActivity extends ListActivity {
private Cursor cursor;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
fillData();
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
}
private void fillData() {
TermsData_DataHelper termsDataHelper = new TermsData_DataHelper(
TestActivity.this);
cursor = termsDataHelper.fetchAllCategories();
startManagingCursor(cursor);
String[] names = new String[] { "name" };
int[] to = new int[] { R.id.label };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter categories = new SimpleCursorAdapter(this,
R.layout.terms_row, cursor, names, to);
setListAdapter(categories);
}
}
When I click on a row in the listview, I don't see any Toast. Can anybody tell me where am I making a mistake.
Thanks in advance.

I was having a similar problem and found that after removing android:clickable="true" from my list items, the onListItemClick() was being triggered just fine. I hope this helps.

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String mString = parent.getItemAtPosition(position).toString();
//will give you the text of current line or
int i = parent.getItemAtPosition(position);
//if you want just position number
}

The solution is to override protected void onListItemClick (ListView l, View v, int position, long id) method.

I had this problem also. I solved it by Removing this from my TextView in the the layout XML.
android:textIsSelectable="true"

Related

Adding onListItemLick/onItemLongClick in a ListFragment

SOLVED IN A COMMENT: Adding onListItemLick/onItemLongClick in a ListFragment
The problem was generated from an ImageButton in the layout of the single element in the ListFragment that was stealing the input even with the focusable element set to false, so i had to remove it from the layout.
I've got a ListFragment populated by an extended class of SimpleCursorAdapter (mainly for overriding the newView method) but I wanted to add an AlertDialog when the user presses (or long presses) an item in the list the adapter generated.
I've tryed both onListItemClick and onItemLongClick with a simple log write using Log.d method but nothing happens in both cases and i don't know where is the problem since the code is really simple:
public class FragmentD extends ListFragment {
private SQLiteDatabase db;
#Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myDatabase myDBHelper = new myDatabase(getActivity());
db = myDBHelper.getWritableDatabase();
Log.d("DB", "Insert fatto");
String[] res_columns = new String[] {myDatabase.COLUMN2, myDatabase.COLUMN2,};
String sortOrder = myDatabase.COLUMN1 + " DESC";
String where = "*";
Cursor testCursor = db.rawQuery("select * from " + database.DATABASE_TABLE, null);
myAdapter adapter = new myAdapter(getActivity(),
R.layout.list_element,
testCursor,
res_columns,
new int[] { },
0);
setListAdapter(adapter);
}
#Override
public void onListItemClick (ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
Log.d("CLICK", "pressed");
}
Edited Answer:
Maybe the components of the layout were stealing the input from other components of the layout
I have got two solution
Simply Override onListItemClick
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("position",position);
}
On the root layout. Add this code
android:descendantFocusability="blocksDescendants"
Hope it helps
Add the super constructor and override it:
#Override
public void onListItemClick (ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
Log.d("CLICK", "pressed");
}
EDIT: try this code
public class FragmentD extends ListFragment {
private SQLiteDatabase db;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(YOUR LISTFRAGMENT LAYOUT HERE, container, false);
myDatabase myDBHelper = new myDatabase(getActivity());
db = myDBHelper.getWritableDatabase();
Log.d("DB", "Insert fatto");
String[] res_columns = new String[] {myDatabase.COLUMN2, myDatabase.COLUMN2,};
String sortOrder = myDatabase.COLUMN1 + " DESC";
String where = "*";
Cursor testCursor = db.rawQuery("select * from " + database.DATABASE_TABLE, null);
myAdapter adapter = new myAdapter(getActivity(),
R.layout.list_element,
testCursor,
res_columns,
new int[] { },
0);
setListAdapter(adapter);
return v;
}
#Override
public void onListItemClick (ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
Log.d("CLICK", "pressed");
}

Android sqlite delete from listview

Im having problems with deleting items from listview(and database). So far i have followed this example:
http://www.vogella.com/articles/AndroidSQLite/article.html but i dont like the delete there (button that always delets first one).
Here is my activity class:
public class FirstActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
final ShoppingSQL shoppingSQL = new ShoppingSQL(this);
List<ShoppingData> list = shoppingSQL.getAll();
ArrayAdapter<ShoppingData> adapter = new ArrayAdapter<ShoppingData>(
this, android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
Log.w("DELETED", " DELETED");
shoppingSQL.delete((int)id);
return true;
}
});
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.w("CLICKED", "CLICKED");
}
}
As you can see i have the listner for long clicks set up and also the delete method that requires an ID. The probelm is with the ID, the one im giving it at the moment seems to be just the order number (0, 1, 2, 3) - not the actual id in db. So, my question is how do i get the real id?
You Can get your Id of Shoping data by
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
Log.w("DELETED", " DELETED");
//here You can get your id by
list.get(position).getID();
//getID id the getter method of shoppingdata ,here you can declare your method for ID as whatever you have in shopping data
shoppingSQL.delete((int)list.get(position).getID());
return true;
}
});
For delete items
adapetr.remove(list.get(i));
then call notifydatasetchanged on listview
You can get the item that is selected from the ListView and then check its ID from the List as follows,
String selectedItem =(String) (MyListView.getItemAtPosition(position));
int ItemID = list.indexOf(selectedItem);
shoppingSQL.delete(ItemID);

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

in a Listview on list item click how can i get the title(the name ) of the selected item? android

I have listview that binds from sqlite and groups by KEY_TITLE(field) so i need to get the name of the item that is clicked
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent i=new Intent(this,detail.class);
i.putExtra(DatabaseIN.KEY_TITLE,SOMETHING THAT I NEED!);
//startActivity(i);
startActivityForResult(i,ACTIVITY_EDIT);
}
// maybe something like this string selectedIteme = l.getSelectedItem().getSOMETNIG?
check this tutorial might help you
http://www.vogella.de/articles/AndroidListView/article.html
you can pick the selected item value like this
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = (Cursor) arg0.getAdapter().getItem(arg2);
Intent i=new Intent(getApplicationContext(),Xyz.class);
i.putExtra("abc", c.getString(1));
startActivity(i);
}
Try this in your onClick method..
Map<String, String> selection = (Map<String, String>) listView.getItemAtPosition(position);
String count = selection.get("count");
String title = selection.get("title");

SimpleCursorAdapter and ViewBinder - Binding data to ListView items to be retrieved on click

So I've got a ListView (using a ListActivity) that I'm populating from a SQLiteDatabase. I'm trying to attach the ID (PK) of the row to the view, so that onListItemClick of each list item, I can do stuff with that ID.
I've read that arbitrary data can be set to a View using setTag and retrieved with getTag(I haven't actually had this work successfully yet, so this may be the problem). Here's a pared down version of what I'm using (for simplicity/brevity):
public class Favorites extends ListActivity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FavoritesDB db = FavoritesDB.getInstance(this);
Cursor c = db.fetchFavorites();
startManagingCursor(c);
String[] columns = new String[] { "_id" };
int[] to = new int[] { R.id.word };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.favorite, c, columns, to);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
view.setTag(cursor.getInt(0));
return true;
}
});
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Object wordID = v.getTag();
Toast.makeText(getBaseContext(), "ID=" + wordID, 1).show();
}
}
The ListView is being populated, and the Toast does show up, but it's always "ID=null", so apparently the ID isn't being set in the ViewBinder call to setTag(or isn't being retrieved property with getTag).
This depends on your implementation of R.layout.favorite. If you have this layout contains a parent view with child TextViews for e.g. the tag you set is for the TextViews while the View v received from the onListItemClick() is the parent View. You need to make sure that you receive the tag for the same view you set by using:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Object wordID = v.getChild(0).getTag();
Toast.makeText(getBaseContext(), "ID=" + wordID, 1).show();
}
You probably should get the cursor from the adapter. This way if your cursor gets replaced you are still are still getting a valid cursor.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = adapter.getCursor();
cursor.moveToPosition(position);
String id = cursor.getString(cursor.getColumnIndex("primary key field name in database");
Toast.makeText(getBaseContext(), "ID=" + id, 1).show();
}
NOTE :
your adapter must be declared as a SimpleCursorAdapter other wise you should downcast it.

Categories

Resources