Determine row number when button is clicked in this row - android

I have a ListView and each row contains button. I don't want to assign to each button its own listener object, cause it will create a lot of objects. But the question is how to determine row number or contextual information when button is clicked?

Every View (including Button) can be "identified" via its setId(int) method.
When clicked you will be passed a view to a View.OnClickListener.onCLick(view). Just extract the ID and you'll know which button this is.
When creating a button:
button.setId(1);
and when this button is clicked:
button.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
int id = view.getId();
// do something with the Id
}
});
Update: answer was updated based on #Octavian Damiean's comment: for identifying Views setId() should be used instead of setTag().

using the
listview..setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int rowNumber,
long arg3) {
// TODO Auto-generated method stub
}
});
we can get the clicked row

Related

ListView example program which switches to new activity

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
}
});

Show/Hide button in listview android,

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();
}
});

Android: Is there any way to find out which view was being click

I have a ListViewwith BaseAdapter which its items are LinearLayout. Inside LinearLayout, there're a TextView and a RadioGroup.
I wrote a OnItemClickListener for listview. However, when I clicked the item inside ListView. Nothing response!
I want to know how could I find out which view was being Clicked. Is there any method like Log.setShowClickingView(true)?
First you need to have an event in your Xml that is something like this
android:onClick="myCheckMethod"
Remember you need to declare /write this statement for your button and radio button.
Now In your Java file (Class) declare the method and match it with the view and you will know which view was clicked
public void myCheckMethod(View v) {
// does something very interesting
String Caller = "";
if (R.id.DI == v.getId()) {
Caller = "DI";
} else if (R.id.btnExp == v.getId()) {
Caller = "Exp";
}
Log.i(Tag,"View Clicked was:"+Caller);
}
Edit User requested to know how to know the ID
getResources().getResourceEntryName(int resid);
or
getResources().getResourceName(int resid);
You'll prefer the first one I guess.
When using OnItemClickListener, one of the things returned is the position within the listView of the item that was clicked:
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getBaseContext(), "Item clicked: " + position, Toast.LENGTH_LONG).show();
}
});
In the above example, "int position" represents the item that was clicked. A toast message is displayed with the index of the item that was clicked.

Difference in listview.setOnItemClickListener and row.setOnClickListener

I am creating a custom array adapter, I now want to implement a function which handles clicking the view. I am having two options in mind, but I am wondering if there is a difference in performance/working speed or something?
Option 1, in the arrayAdapter itself:
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Option 2, from the main Activity:
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
Or are they exactly the same?
Or are they exactly the same?
In first approach you will create for each row own listener e.q. you have 100 rows so you'll have 100 listeners that is not good at all. In second approach you will create one listener for whole ListView.
android.widget.AdapterView.OnItemClickListener
android.view.View.OnClickListener
How you can see, first is more comfortable and directly designated for dealing with adapter widgets like ListView is. Also this approach is generally recommended and used.
You have connection with each row via parameters of onItemClick() method and code is more human-readable and it's clearer.

Capturing TextView clicks in ListView

I’ve a custom ListView containing 4 TextViews. Now, the TextViews have to be Linkifiable. Since Linkify wasn’t working in ListView, I made a callIntent function to see if the link is clickable or not. But if there’s no clickable link, I want to start a new Activity. I use the following code:
lvMembersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
final TextView txtLine1 = (TextView) arg1.findViewById(R.id.tv_member_details_line1);
final TextView txtLine2 = (TextView) arg1.findViewById(R.id.tv_member_details_line2);
final TextView txtLine3 = (TextView) arg1.findViewById(R.id.tv_member_details_line3);
final TextView txtLine4 = (TextView) arg1.findViewById(R.id.tv_member_details_line4);
txtLine1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(1);
}
});
txtLine2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(2);
}
});
txtLine3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(3);
}
});
txtLine4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(4);
}
});
}}
Now, when I click on an item for the first time, the TextView listeners are getting set. The individual clicks only works after the second time. What should I do in such a case?
Put this code in the getView() method in the adapter. And in your code onItemClick is called when you click on a row of the listview.
Add the following attribute for each of the textviews(The four textviews in the layout which going to use as the custom list item)
android:onClick="onTextView1_click"
- You have to do this for all the 4 textviews to track the click event seperately-
Then in the code add the following method to trigger when you click the textview1
public void onTextView1_click(View view)
{
final int position = (Integer) view.getTag();
//implementation (Which needs to be done when someone click textview1)
}
Also most importantly You have to add the position as a tag from the getViewMethod in adapter class. this will help to track the clicked item in the listview
holder.textView1.setTag(position);
and you can access this value as I have indicated in the "onTextView1_click(View view)" method "Position variable"
In the List view --->
android:focusable="false"
In the code add the "setItemsCanFocus(true)" soon after initialize list view. This indicates focusable items contains within each item in list view.
lvMembersList = (ListView) findViewById(R.id.NameOfListTView);
lvMembersList.setItemsCanFocus(true); // <---------[X]

Categories

Resources