Can anyone knows how can i make something like this in ListView ?
When a item in ListView is Clicked the imageButton get Visible.
http://img850.imageshack.us/img850/2377/is4t.png
Then you click another item and the previus item that you clicked get invisible , and the actual item get the image button visible.
http://img14.imageshack.us/img14/1920/lnpy.png
I have searched about this , but i haven't found something to work exactly i want. And i have tried to make the button visible onItemClick for that view , but , when i click another button , the previus item still visible. I have tried to do notifyDataBaseChanged but still there. Thanks for help , and sorry for my bad english.
¿How can i get access on the previous Item View to set button to View.GONE ?
Code :
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
// TODO Auto-generated method stub
MPlayer.playSong(position);
Button bPlaying = (Buttton)view.findViewById(R.id.button1);
bPlaying.setVisibility(View.VISIBLE);
ca.notifyDataSetChanged();
}
});
Not the best solution but hope this might help you solving you the problem
private Button previousButton = null;
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
MPlayer.playSong(position);
if(previousButton != null){
previousButton.setVisibility(View.GONE);
}
Button bPlaying = (Buttton)view.findViewById(R.id.button1);
bPlaying.setVisibility(View.VISIBLE);
previousButton = bPlaying;
ca.notifyDataSetChanged();
}
});
Related
Could anyone possibly give me an entire example for demonstrating a ListView.
Each item in listview needs to fire a new activity.
For ex, if I use 'Subjects' as content of ListView then after pressing particular a subject from listview , a new screen with its title and some content of that subject gets displayed.
I know this is too much to ask, but i have been looking for ListView Ex. since quite long and nothing has helped me so far. PLease Help
Thank you
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
then use bundle to send some content to your next activity.
then getActionBar.setTitle(sometext); this text is come from your bundle.
This contains:
http://www.androidbegin.com/tutorial/android-search-listview-using-filter/
you can refer following link for listview demo code.
http://www.androidhive.info/2011/10/android-listview-tutorial/
and for opening new activity on selecting any item, you can use,
listCoupon.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// here you can compare selected item by its position and then start a new activity
// arg2 is selected item position in your arraylist
}
});
In my android application i'm having a list view and some items in that,now my requirement is when I select a particular item from that list the list item should be highlighted with a red color, and that selection should not be disappear because in my application i should choose an item from the list view and i should click a button(submit).
So in order to know to the user that he has selected an item from the list view it should be highlighted until he clicks submit button.
I have used choice Mode attribute and set the value to single choice and i have changed the color for highlighting everything works fine but i need it not to be disappeared until user clicks the submit button
The variable val consists of some names which i retrieved from database.
Please help me to solve this Thanks in advance.
ArrayAdapter<String> adptr= new ArrayAdapter<String>(this,R.layout.row,R.id.member_name,array);
lv.setAdapter(adptr);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
val=lv.getItemAtPosition(pos).toString();
//Toast.makeText(getApplicationContext(), val, 5000).show();
}
});
use OnItemClickListener
ListView lview = getListView();
int pos =0;
lview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// set the background of v here
if (position == pos)
// reset the background color
// and pos = position;
else
get the previous listitem view and reset it's background to original
}
});
Actually I had the same problem sometime back. I was working on non touch andorid ui application for google tv.
The solution is like this.
1) create a custom ArrayAdapter (extending ArrayAdapter)
2) onItemClick get the position of the item.
3) send that position to your adapter by some public method say setCurrentPosition(int pos)
4) in getView() of the adapter check for the position and set the background to red for that view.
Hope this will work as it worked for me.
I have 20 buttons in vertical layout in a scrollable window. While click on a button I want to know the position of that button on screen.Some times I click the button after a scrolling.I want the exact position at that time too.What is the code for that.please help me.
Never use 20 buttons inside layout. Use listview with button and use onitem click of listview and get button position:
list view onitem click:
listId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
-------Here position displays **pos**----
}
});
Refer this example for a nice listview sample
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println("this is position"+lv.getItemAtPosition(arg2)
}
});
as we know using android grid view, we can do the following and get notified when item is clicked:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(PopularCapitActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
we also know that, if the cell in the grid contains a clickable item, say a button, the above won't get fired.
so currently i have a grid view, each cell has its own button, so now when user clicks on the button, it will have its own action based on the cell the button resides in, my question is, how can i access the cell position in the button handler?
thanks
Assuming you are using a custom adapter for the GridVIew, in the getView method you can simply add a tag to the Button object that contains the position passed into getView:
button.setTag(new Integer(position));
Then, in the onClickListener method, with the view that is passed in (the button) you can do:
Integer position = (Integer)view.getTag();
And then handle the position value from there.
EDIT:
It appears the best practice would be to do:
button.setTag(Integer.valueOf(position));
rather than using the Integer constructor.
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String a = String.valueOf(position);
Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
}
});
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".