Handle ListView/ListViewItem click in layout.xml onClick - android

Usually, I assign button click handler as follows-
//activity_main.xml
<Button android:onClick="DoWork" />
//MainActivity.java
public void DoWork(View view){}
Now, I have got a ListView which click event handler is as the following-
protected void onCreate(Bundle savedInstanceState) {
final ListView listView = (ListView) findViewById(R.id.CategoryListView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
}
});
}
If I would like to do <ListView android:onClick="Something"/> , how should I define the method?
Sorry if this is a foolish question. Thank you.

Related

Android List Activity not working well

Hello everyone,
I have a little problem regrading to list activity. I want to create a simple list of ( 1 , 2 , 3 , 4 , 5 ) and when I clicked on them a Toast will pop up and says clicked. But the application is not running. When I remove list (extends activity rather then ListActivity ) . The app just simply runs and shows a list. I want to apply OnlistItemClick. Hope you help. Here is xml and java code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.annotationap.MainActivity$PlaceholderFragment" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="256dp" >
</ListView>
</LinearLayout>
java code
public class MainActivity extends ListActivity {
private String[] array = {"1", "2" ,"3","4","5"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView1;
listView1 = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> aa = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, array );
listView1.setAdapter(aa);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(this,"Clicked" , Toast.LENGTH_LONG).show();
}
}
I think its not a big problem, you should search first before posting,
just change your id of listView in XML file
android:id="#android:id/list"
if you're going to use ListActivity then you must have to care about the listView's ID, you can change it but then you'll have to use simple Activity else than ListActivity.
It sounds like you want to implement an AdapterView.OnItemClickListener in a normal Activity. In that case, here's a quick example:
public class MainActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.listView1);
list.setOnItemClickListener(this);
list.setOnItemLongClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "Long clicked!", Toast.LENGTH_SHORT).show();
return true;
}
}
You are not setting a listener for listView1. In onCreate you want to set something like this:
listView1.setOnItemClickListener(listener);
However are a few ways you can do this:
I always do the following:
protected void onCreate(Bundle savedInstanceState) {
//Standard onCreate stuff
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
//this will run on click.
}
});
How can i attach an listener to listview?
Info on listeners:
http://martin.cubeactive.com/android-onclicklitener-tutorial/

setOnclicklistener for each Button in Listview

I am facing a problem with android listview. I have multiple Buttons inside my ListView and I want to setclicklistener for each Buttons and retrieve their position in ListView.
ListItem.xml
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/btnList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff" />
MainActivity.java
mDrawerList.setAdapter(new ArrayAdapter<String>(
getApplicationContext(), R.layout.drawer_list_item,
mPlanetTitles));
button OnClickListener
btnList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
new DrawerItemClickListener();
}
});
class DrawerItemClickListener
private static class DrawerItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
MainActivity main= new MainActivity();
main.selectItem(position);
}
}
Use this and the position in OnItemClick is the button's position in the list
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
}
You need to handle the on click listener of the button inside the getview of the listview adapter, where you find the button using view.findViewById(R.id.yourButtonID).
This is how you can do it :
Button yourButton= (Button) rowView.findViewById(R.id.yourButtonID); // be carefull to use the view of the listItem and not the activity in case the adapter is inside the Activity.
yourButton.setTag(position);// Any data associated with the button has to be added with setTag()
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Log.w("DEBUG_TAG", "Button Clicked at position : " + position);
//here you must use getTag() in order to extract the data set in the setTag()
}
});

Set Text based on list item click

I am having a layout consisting of ListView and a label. Look at the image below.
I implemented listview using base adapter in a seperate .java file.
Could anyone suggest me how can i set text to the label on click of list item?
EDITED :
The text of the label should be number of list items clicked.
Suppose i clicked a button in a list item, the label should be set to 1.
Similarly in the next attempt if i clicked another list item's button it should be set to 2 and so.. on..
Sir,
What you are saying is this, you have your adapter in one class and the activity in another file. Well you could do this, to update the textview.
pass the context to the activity, and if its in the adapter you are maintaining the count then once the count has been updated,
then, assume you have this method in the activity
public void updateTextView(int count) {
// enter your code here to set the count in the textview
}
and from the baseAdapter call the above method like this:
if(mContext != null) {
((YourActivity)mContext).updateTextView(mCount);
}
and the textview in the activity will be updated!
I hope it helps.
In your OnItemClickListener call adapter.getItem(int position) to retrieve the object from the collection backing your BaseAdapter. From there, you should be able to retrieve any fields you need.
Edit:
Your edit clears up the question. Updated answer:
private int mCounter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ListView listView = getYourListView();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCounter++;
updateTextView();
}
});
if(savedInstanceState != null) {
mCounter = savedInstanceState.getInt("counter", 0);
}
updateTextView();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("counter", mCounter);
}
private void updateTextView() {
// TextView textView = getYourTextView();
textView.setText(String.valueOf(mCounter));
}
Just OnItemClickListener in your ListView then in the onClick you will get a value arg2 which is the position of the item which is clicked.
Just get that value from the ArrayList from which you are displaying the ListView and show it...
Hope this is what you need if I have not misunderstood your question.
try this code
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String itemText=(String) arg0.getItemAtPosition(arg2);
yourLable.setText(itemText);
}
});
In onListItemClick call list.getItemAtPosition(position) to retrieve item text
then set this text to textview1.setText(listText).
First Implement your onItemClickListener, where you will get int arg2 parameter, which is position, so get that position and do your stuff whatever you want like below.
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
LABLE.setText(""+arg2);
}
});
Check this code
public class ListA extends ListActivity {
private TextView selection;
private static final String[] items={"Item 1", "Item 2", "Item 3"};
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_list);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items));
selection=(TextView)findViewById(R.id.selection);
}
#Override
public void onListItemClick(ListView parent, View v, int position,
long id) {
selection.setText(position);
}
}

Android ListAdapter not working with the onItemClickListener function

I have the below code, but the onItemClickListener doesn't work,
can anyone help me what the problem could be?
I have also added the Override and the setClickable but the problem still exists.
public class Show extends Activity implements AdapterView.OnItemClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.show);
...//Some codes in here
adapter = new MyAdapter(this, hadithList);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
});
}
In your Activity, you are first implementing AdapterView.OnItemClickListener and than you are setting AdapterView.OnItemClickListener to your ListView. You should choose one of the methods, not both.
If you choose to stick up implementing OnItemClickListener, you should add this to your Activity :
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
}
and add this to your ListView : list.setOnItemClickListener(this);
If you choose to not implement OnItemClickListener just do :
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
}
});
This should work properly.

2 ListViews in the same activity

If I have 2 or more listviews in one activity,then how do I use a onclicklistener? I mean How do I know on which one of them the user click?
public void onItemClick(AdapterView parent, View v, int position, long id) {
}
The above code is what I used,however when I try to use another listview,I just can't find a way to detect which listview is clicked.
Any ideeas to solve this?
In this case, the parent is the listView from which the itemClick originated. So what you can do is keep a member variable for each ListView and compare the parent to those members to see which list triggered the click.
So here's a simple class with what I mean:
public class MyTest extends Activity{
private ListView list1;
private ListView list2;
public void onCreate(Bundle b){
super.onCreate(b);
list1 = new ListView();
list2 = new ListView(); //or findViewById if you declared them in your layout
//the rest of your creation code here
}
public void onItemClick(AdapterView parent, View v, int position, long id) {
if(list1 == parent){
//handle list1 click
}else{
//handle list 2 click
}
}
}
There are two ways you can do it.
Implement OnItemClickListener
public class ListViewTest extends Activity implements OnItemClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
if(view ==myListView)1{
}
if(view ==myListView){
}
}
}
Set your own listener
myListView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO: click on second listview
}
});
You can do it as this:
listView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO: click on first listview
}
});
listView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO: click on second listview
}
});
its pretty simple ,
only one list can act as the official list under a ListActivity and this list (and only this list) should have the special list id (#android:list i think) so just set the id of the other list to some other id and set its setOnItemClickListener to do whatever you want. I currently work on an app with 2 listViews and an additional list Fragment.

Categories

Resources