listView item click event not firing - android

I am having an issue with using a listview that when I click it nothing is happening. I would like for it when I click the listview item to make a toast so I know it is being clicked. I have been trying/researching for a while and nothing. Would anybody mind taking a look to see if I am missing something I just am overlooking? Many thanks in advance!
Here is my class:
public class MyCar extends Activity {
/**
* Called when the activity is first created.
*/
public ListView mylistView;
String carInfo;
private ArrayAdapter<String> mylistAdapter;
ArrayList<String> arrayListCar = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mycar);
mylistView = (ListView) findViewById(R.id.listView);
arrayListCar = new ArrayList<String>();
//Just had to remove setting this adapter 2 times. Took out line below to fix.
mylistView.setAdapter(mylistAdapter);
mylistView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
carInfo = trySomethin();
fillList();
}
public void fillList() {
String make = "";
String model = "";
String[] pieces = carInfo.split("\"");
make = pieces[3];
model = pieces[7];
ArrayList<String> carList = new ArrayList<String>();
carList.add(make + " " + model);
// Create ArrayAdapter using the car list.
mylistAdapter = new ArrayAdapter<String>(MyCar.this, android.R.layout.simple_list_item_single_choice, carList);
mylistView.setAdapter(mylistAdapter);
mylistAdapter.notifyDataSetChanged();
}
}

if you have any elements on listview item change this for them
android:focusable="false"
and if you are changing any elements visibility on runtime you have to handle focus programatically each time you change its visibility.

Try this
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});

Related

How to find the position of item in listview

I have created a Custom listView where i put some text and image but when i clicked in list view their is no response.I have created a custom Listview.
if any one known then please tell me.
my code is here
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<pdf> pdfList;
//the listview
final ListView listView;
//initializing objects
pdfList = new ArrayList<>();
listView = (ListView) findViewById(R.id.list);
//adding some values to our list
pdfList.add(new pdf("Book1", "First Year"));
pdfList.add(new pdf( "Book2", "First Year"));
//creating the adapter
ListAdptot adapter = new ListAdptot(this, R.layout.customlistview, pdfList);
//attaching adapter to the listview
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){
if(position==0)
{
Toast.makeText(MainActivity.this, "Position 0", Toast.LENGTH_SHORT).show();
}
else if(position==1)
{
Toast.makeText(MainActivity.this, "Position 1", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
Get list item positon click as below
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){
String value = listView.getItemAtPosition(position).toString();
}
}
);
If you have any clickable views inside your list item, then it will consume click event of your ListView item click.
Set android:focusable="false" on your Button or ImageButton if you have any.
Reference SO answer

list item select set bgcolor but scroll down automatically list item set bgcolor changed

particular list item select to set bgcolor for list item ,when scroll down listview to automatically set color for other item, my sample code suggestion and help me.(this code v.setbackgroundcolor(Color.gray) using problem occur,so clear the problem)
public class MainActivity extends Activity {
ListView list;
ArrayAdapter<String> adapter;
ArrayList<String>listitem=new ArrayList<String>();
String val;
String[] list_number={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.listView1);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list_number);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
String selectedFromList =(list.getItemAtPosition(position).toString());
if(listitem.isEmpty())
{
listitem.add(selectedFromList);
Set<String> primesWithoutDuplicates = new LinkedHashSet<String>(listitem);
listitem.clear();
listitem.addAll(primesWithoutDuplicates);
v.setBackgroundColor(Color.GRAY);
}
else
{
if( listitem.contains(selectedFromList))
{
listitem.remove(selectedFromList);
v.setBackgroundColor(Color.WHITE);
}
else
{
listitem.add(selectedFromList);
Set<String> primesWithoutDuplicates = new LinkedHashSet<String>(listitem);
listitem.clear();
listitem.addAll(primesWithoutDuplicates);
v.setBackgroundColor(Color.GRAY);
}
}
}
});
}
}

Android: how get ID of an item selected in a spinner?

I have this private method to render a spinner in a fragment:
private void renderSpinner() {
List<String> spinnerArray = new ArrayList<String>();
for (int i = 0; i<mPromotionList.size(); i++) {
ModelPromotion modelPromotion = (ModelPromotion) mPromotionList.get(i);
String name_promotion = modelPromotion.getName();
int id_promotion = modelPromotion.getIdPromotion();
spinnerArray.add(name_promotion);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mBaseApp, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerPromotion.setAdapter(adapter);
mSpinnerPromotion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String name_promotion_selected = parent.getItemAtPosition(pos).toString();
//TODO REMOVE
Log.d(LOGTAG, "Il nome della promo selezionata รจ " + name_promotion_selected);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
As you can see, I have "ready" the id_promotion (It's the real value that I need)... And I know that I'm not adding (for now) at the List.
How I can get it, inside the onItemSelected?
Thank you very much
You can use the position of the selected item and use that to get the ID from the original list.
((ModelPromotion) mPromotionList.get(pos)).getIdPromotion()
Another option is to create a custom ArrayAdapter subclass that will accept ModelPromotion instead of String and override getItemId to return the idPromotion
An example about custom ArrayAdapter on Github.

Undo actions in a Listview - onitemclicklistener

this is my code; basically, it is a listview with multiple choice that add a number to a score (textview) when its clicked. My problem is that i would like the number be added to the score when the item is selected in the listview, and removed when the item is deselected. With my code each time that you click an item, the number is added to the score.
public class MainActivity extends Activity {
ListView myList;
TextView tv1;
String[] listContent = {
"Add 2 to total",
"Add 3 to total",
"Add 1 to total",
"Add 5 to total",
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = (ListView) findViewById(R.id.listView1);
tv1 = (TextView) findViewById(R.id.textView);
final int[] counter = {0};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listContent);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
private String[] listview_array2 = {"2", "3", "1", "5"};
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (myList.isItemChecked(position)) {
String name = this.listview_array2[position];
int counter2 = Integer.parseInt(name);
counter[0] = counter[0] + counter2;
tv1.setText("Total " + counter[0]);
} else {
}
}
});
}
}
I have thought that maybe changing to setOnItemSelected instead of ItemClicked, but I am not sure.
Hope I have explain myself properly and you can help me.
Thank you in advanced.
Try with OnItemClickListener with a Boolean for selection/deselection.
Hope this will work
You can do that through your code.
Maintain a flag in the list of your values like List list;
Values is a model containing the number and a flag.
If a item is clicked, set the flag true and do your calculations.
If already clicked item is clicked again, you just need to check the flag.
if(list.getposition(position).isItemClickd){
// Decrease that value from total
// list.getposition(position).isItemClickd = false;
} else{
// list.getposition(position).isItemClickd = true;
// total = total+prevValue;
}
I finally made it in a easy way!!!
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (myList.isItemChecked(position)) {
String name = this.listview_array2[position];
int counter2 = Integer.parseInt(name);
counter[0] = counter[0] + counter2;
tv1.setText("Total " + counter[0]);
I add in "else" the same action than in "if", but subtracting the number

how to add action to arraylist item in listview and how to get its position?

Hi I am working with android.I had created a listview using array list. Now how can I add Action to each list item and also how to get the list position of that item. Please help me, thanks
here is my code
public class MainActivity extends Activity {
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.listView1);
ArrayList<String> list=new ArrayList<String>();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list);
lv.setAdapter(arrayAdapter);
}
}
You need to implement ListView setOnItemClickListener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
For more information go to Documentation.
you can also get the selected item of ListView in setOnItemClickListener using following code:
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
int itemPosition = position;
String itemname = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), "Item: " + itemname + " is at position: " + itemPosition, Toast.LENGTH_LONG).show();
}
});
As the listview cells position and the index of arraylist are same. So we can easily get the item from arraylist by the position of cell of listview.
listView1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
int position=position;
String item=marraylist.get(position).toString();
}
}

Categories

Resources