List view select items in custom layout - android

I have a listview, in each row have five button, but I do not know as get focus over each button, someone know ?

You can do like this
Add tag value to that five button in its Xml file like 1,2,3,4,5
int rowNo;
int buttonTag;
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
rowNo = position; // which row is selected...
buttonTag = v.getTag(); // which button is selected... if 1 then first if 2 then second
}
});
I hope this is what you want..

Related

Is there a way to store the ListView item number in an int in Android?

I imported a text file into a ListView in android.
If I click an item in listView, it's line number should be stored in an int.
For example (this is a ListView (text file)):
Corn (1)
Apple (2)
Melon (3)
If a line is clicked for example Melon, it's line number (3) should be stored in an int.
The code:
// returns index to -1
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
boolean co = true;
int lineNumber = list.getSelectedItemPosition();
Toast.makeText(MainActivity.this, String.valueOf(lineNumber), Toast.LENGTH_SHORT).show();
}
});
Is that possible?
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(MainActivity.this,"Item No:"+position,Toast.LENGTH_SHORT).show();
}
});
Note Item index start with 0.
Method like this may also help you
listView.getSelectedItemPosition()
listView.getCheckedItemPosition()
Note: This above two line is not meant for item click.
First of all, ListView index starts from zero, so the index should be like this
Corn (0)
Apple (1)
Melon (2)
And easily you can get the position of the selected item as it's declared as a parameter in
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
And also you can get the name of the selected item by this line in onItemClick()
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// TODO: Implement this method
String title = parent.getItemAtPosition(position);
}
I just used int position included in the method:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();

Highlight current touched item as selected item

I have a android file explorer application and I need to highlight the selected item. I could select touched items using v.setBackgroundColor() method inside onListItemClick(). But when I touch another file/folder still previous one highlighted. I need only current touched item display as selected. How to do this?
not perfect but works good
loop through all items of list and try something like this
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
for (int i = 0; i < arg0.getCount(); i++) {
View view = arg0.getChildAt(i);
if (i == arg2) {
view.setBackgroundColor(Color.RED);
} else {
view.setBackgroundColor(Color.GREEN);
}
}
}
});

how to know if item in listview is pressed/geting listview item from id

I have populated a list view. which show up like
123 apple
456 Linux
789 windows
I have also added onItemClick listener to it and by displaying toast I can check the id of pressed listview. As I have a long list of approx. 1000 items, I want that if item 1 is clicked I should get the listview. some thing like
String clicked_Item = 123 apple.
how can I do it. here is my itm listener.
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long id) {
Toast.makeText(getActivity(), "item clicked id="+id, Toast.LENGTH_LONG).show();
}
});
In your adapter, you should have a method called getItem(position)
So, in your onItemClickListener, the int arg2 means the position of the list that was clicked.
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long id) {
Object object = adapter.getItem(position);
Toast.makeText(getActivity(), "item clicked id="+id, Toast.LENGTH_LONG).show();
}
});
So, this object might be your model, wich means that this object has every information you want. So, just take what you want.

How do i get Last TextView's Data from Row of customlistview?

I want to get Last textview from row that was being clicked by user
for ex,
I have clicked on first row then i will get "Single Cheese Topping"
So How Do i implement the onclicklistener any suggestion please.
For listview, you should use OnItemClickListener instead of OnClickListener. Here's you go.
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View viewGroup, int position,
long arg3) {
TextView lastTextView = (TextView)viewGroup.findViewById(R.id.last_textview_id);
String lastText = lastTextView.getText().toString();
}
});
Second parameter return your row layout ViewGroup. You just use findViewById to get the view you need to use from the row layout.
lv1.setOnItemClickListener(new OnItemClickListener (){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3)
here arg0 has the name of the item at index presses. you can get the name like this
name=arg0.getItemAtPosition(position).toString();
and you are done

change background color on list item selection android

hello frnds i want to change background color (white on selection) of list on selection of list in listview and if i select any other position then first selected row comes to its previous state and currently selected rows background become white.so how to do this
public void onListItemClick(ListView parent, View v, int position, long id) {
super.onListItemClick(parent, v, position, id);
//do some stuff here
Toast.makeText(getApplicationContext(), "You have selected"+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
I wouldn't do that in code, since you later on might want to change colors, and you shouldn't have "layout/styling" code hardcoded.
Do instead create a style, and apply that to the ListView in your xml. You can read about how you do that in this thread:
ListSelector applies to the entire list
Your list view click listener:
yourlistView.setOnItemClickListener(new OnItemClickListener() {
#Override public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
yourAdapter.toggleSelected(position);
yourAdapter.notifyDataSetChanged();
}
});
Then make an ArrayList in your adapter and initialize it to store all the positions of your list view items:
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
int length = yourmainarraylist.size();
for(int i = 0; i < length; i++){
selectedIds.add(0);
}
then put a check in getView to toggle the background:
if (selectedIds.get(position)==1)
convertView.setBackgroundResource(R.drawable.list_row_selected);
else
convertView.setBackgroundResource(R.drawable.list_row);
and put this method in your adapter
public void toggleSelected(int position) {
selectedIds.set(position, (selectedIds.get(position) == 0));
}

Categories

Resources