ListView Select Problem - android

I have a listview in my program. the problem is that. the list is in single choice mode and when i select any item i have 4 buttons on the footer of my page for that item.. alll i want is to know that. how can i keep that item selected and while cliking any button i can get the selected item.

Try this:
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView a, View v, int position, long l) {
arrayPosition=position;
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
where arrayPosition is a static integer variable.
Try this if it helps..

If you are using a ListActivity you can use getSelectedItemId() or getSelectedItemPosition(). Alternatively you can bind your own OnItemClickListener to the ListView object. More information can be found in this tutorial.

Related

setOnItemclicklistner for simplelistitem1 or simplelistitem2

I am using android built in layout android.R.layout.simple_list_item 2. I wanted to perform onItemclicklistner() for the items in this layout. I couldn't find the resource id for this layout without which I couldn't find a way to perform the listner function. The examples I have seen so far is
ListView list = (ListView) findViewById(R.id.mylist); // Since I am using built in layout I couldn't figure out the resourse id as in this case.
list.setOnItemClicklistner();
So my problem is without knowing the resourse id of the built in layout "simple_list_item 2" how can I create a Listview object. and without having a ListView Object I am unable to access setOnItemClickListner(). Hope I delivered the question in a meaningful way. Thanks
Use setOnItemClickListener() instead of setOnClickListener()
If you want to perform clicks on items in the ListView, try using OnItemClickListener in place of OnClickListener. The OnItemClickListener can be set on the ListView as shown below.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Perform the required tasks here
}
});
for list item click u should set onItemClickListener like below
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
}
});
also you can set onItemClickListener Interface to your Activity and override method in activity
use setonItemClickListner();
listview.setOnItemClickListner(new OnItemClick(){});

How to disable all elements (also the ones not currently visible in the screen) in a listview?

I have a ListView with a set of elements. When I click one of the I would like to disable all the others. If I click again on the selected item all the other items are enabled again.
I tried the different proposed solution without success. I hope someone can help me.
This is my code:
//action to take when a presentation is selected
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
//disable the other items when one is selected
if(position!=selectedPresentation){
for(int i=0;i<parent.getCount();i++){
if(i!=position)//disable all the items except the select one
parent.getChildAt(i).setEnabled(false);
}
selectedPresentation=position;
}else if(selectedPresentation==position){
//enable again all the items
for(int i=0;i<parent.getCount();i++){
parent.getChildAt(i).setEnabled(true);
}
selectedPresentation=-1;
}
Where selectedPresentation is a global variable storing the selected item. If no item is selected its value is -1.
Thank you for your help!
Make your own subclass of ArrayAdapter that has AreAllItemsEnabled() return false, and define isEnabled(int position) to return false for a given item in your the ones you want to disable.
In your custom ArrayAdapter overide isEnabled method as following
#Override
public boolean isEnabled(int position) {
return false;
}
other option
Don't implement onItemclickListener. This will not give you any update of item click. Only register onClick listener to views.
You may take another object of the List(Arraylist) which is populating elements in listview then on click copy the corresponding position data to new arraylist you made and then notifyDataSet and when you click again you populate listview with original list so it will apear again....Just do this trick it might work for you
parent.getChildeAt() only work for visible Item.
you should change something in adapter.
if make custom adapter then you can do some thing like #ṁᾶƔƏň ツ answer, but if you use default adapter you can change adapter's arraylist that before you pass it to adapter.
put boolean in it (in arraylist) and in on item click true/false it for all item.
I wrote this solution and seems it works fine!
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
//disable the other items when one is selected
Toast.makeText(context, "onClick called",Toast.LENGTH_SHORT).show(); // this will flash up twice
if(position!=selectedPresentation){
selectedPresentation=position;
for(int i=0;i<adapter.getCount();i++){
if(i!=position)//disable all the items except the select one
adapter.isEnabled(i);
}
}else if(selectedPresentation==position){
//enable again all the items
selectedPresentation=-1;
for(int i=0;i<adapter.getCount();i++){
adapter.isEnabled(i);
}
}
And in my adapter I wrote:
public boolean isEnabled(int position) {
if(selectedPresentation==-1 || selectedPresentation==position)
return true;
return false;
Now my concern is how to show the items in the listView as disabled

List.Remove always removes last item for ListView

When i try to remove a specific item from a list View:
buyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tempToken -= selPerk.cost;
plrPerks.add(selPerk);
String tokStr = String.valueOf(tempToken);
tkn.setText(tokStr);
shopItems.remove(selPerk);
selPerk = new Perk();
perkDialog.dismiss();
}
});
It always seems to remove the last item. This is where i open the dialog:
perks.setClickable(true);
perks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Perk perk = (Perk) perks.getItemAtPosition(position);
showItem(perk);
}
});
}
This is the show Item function:
public void showItem(Perk perk) {
if (tempToken >= perk.cost) {
selPerk = perk;
How do i remove a specific item from a list and list view respectively?
Thanks for your time :)
In "setOnItemClickListener" listener, you are getting the perk object. So you can remove that object from your list like this-
shopItems.remove(perk);
and then you can call-
your_adapter.notifyDataSetChanged();
to refresh your listview.
To remove a specific item from a list view, your can call removeView(View toBeRemoved) if you have a reference to the view you wish to remove. If you have the index, you can call removeView(int index).
http://developer.android.com/reference/android/widget/ListView.html
You can remove a specific item from a list in the same way, using remove(Object item) or remove(int index).
http://docs.oracle.com/javase/7/docs/api/java/util/List.html
Hope this helps!
I fixed it. Whenever i removed an item i had to do it like so:
shopItems.remove(selPerk);
perk_adapter.notifyDataSetChanged();
So i had to notify my listeview adapter that i removed an item.

How to get a particular row in listview to perform its own operation?

I am new to android programming. I need some help here. I have used this site example on creating a listview. What I want to achieve is when the user clicks a particular row, the row clicked will perform its respective action. (Eg. When clicked row 1 will show a toast. When clicked row 2 will direct the user to another new view, etc.)
I have set a OnItemClickListener to the listview but am lost on how to do it. Any help will be appreciated. Thanks!
Below is my code:
.......
final ListView list = new ListView(this);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(Adapter<?> arg0, View v, int i, long l){
// At implementation
}
});
.......
It looks like you're on the right track, everything should be clearer with onItemClick's parameter names :
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked row " + position,
Toast.LENGTH_SHORT).show();
}
So you actually get the position of the item clicked, and can have a different action for each item.
To get the index of the selected item:
int selected_item = myListView.getPositionForView(view);
or to get the string:
String chosen_item = (myListView.getItemAtPosition(selected_item).toString());
If your class is extending ListActivity replace "myListView" with "this".

Need help to delete selected item from listview

I want to delete a selected item from the list view;
actually I want to perform this operation from the context menu. Everything is going fine but I'm not able to delete that item.
Please give me some suggestions or examples to remove item from the listview
I have used like this in my code, it can delete multiple items from the list
ListView lv_ArchivePartylist;
ArrayList<Parties> select_archived_party;
lv_ArchivePartylist = (ListView)findViewById(R.id.archive_ListView01);
lv_ArchivePartylist.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
if(view.findViewById(R.id.img_chkbox_archive).getVisibility()==TextView.GONE)
{
view.findViewById(R.id.img_chkbox_archive).setVisibility(TextView.VISIBLE);
Toast.makeText(ctx_archive, "Name="+archived_parties.get(position).getPartyTitle(), Toast.LENGTH_SHORT).show();
select_archived_party.add(archived_parties.get(position));
}
}
});
Then I've declared one button of "Delete" and on it's On ClickListener method, it calls the code from the database(In your case it may be Arraylist or array) to delete the items selected in Arraylist "select_archived_party". Hope it helps :-)

Categories

Resources