I am trying to make a listView with two buttons on each row. I have an adapter where the buttons and the rest views of my list are declared. My problem is that I can not fire the buttons from my main activity. I thought the code below should work but it didn't.
public class Zmenu extends Activity {
final EfficientAdapter Myadapter=new EfficientAdapter(this);
final ListView l1 = (ListView) findViewById(R.id.ListView01);
l1.setAdapter(Myadapter);
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch(arg1.getId()) {
case R.id.Button1 :
//do this block
break;
case R.id.Button2 :
//do this block
break;
}
}});
}
Can anyone helps me on what I am doing wrong in how could I fire the key listener in my main activity?
Either you will need to let the events from the button percolate to the parent listview (see How to pass the onClick event to its parent on Android?) or in your EfficientAdapter's getView() function, you need to register the listener with each button in the item.
EDIT: you don't have the correct code layout. You need to implement your code in an onCreate function
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//here goes your layout
final EfficientAdapter Myadapter=new EfficientAdapter(this);
final ListView l1 = (ListView) findViewById(R.id.ListView01);
l1.setAdapter(Myadapter);
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
switch(arg1.getId()) {
case R.id.Button1 :
//do this block
break;
case R.id.Button2 :
//do this block
break;
}
}
});
}
Related
I'm new to android development. I have 2 ListViews. when I click on one item on the first ListView, the new dataset will show in the second one. I have added a Button to the second ListView (onItemClick). Using an Adapter. So when I click on the Button (Read more) it will load a new Activity. So when I click on the Back Button i need to load the same data(listview2) in my previous step.
int images[] ={R.drawable.boc, R.drawable.commercial, R.drawable.nations, R.drawable.popls};
adp = new ItemsAdapter(getActivity(), images);
menu.setAdapter(adp);
menu.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv2 = (TextView) v.findViewById(R.id.listViewText);
/*ImageButton imgbtn = (ImageButton) v.findViewById(R.id.AddButton);
ImageButton imgbtn2 = (ImageButton) v.findViewById(R.id.AddInfo);
*/
switch (arg2) {
case 0:
ListAdapter adapter3 = new ListAdapter(getActivity(), boc) ;
menu2.setAdapter(adapter3);
break;
case 1:
// menu2.setAdapter(new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1,subitems2));
ListAdapter adapter4 = new ListAdapter(getActivity(), bankcrcards) ;
menu2.setAdapter(adapter4);
break;
default:
break;
}
}
});
menu2.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ImageView imgbtn1 = (ImageView) arg1.findViewById(R.id.imageView2);
imgbtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
expandableListView=new ExpandableListFragment();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.setCustomAnimations( R.anim.fade_in, R.anim.fade_out);
transaction.replace(R.id.myFragement,expandableListView);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
});
}
});
Can I propose that instead of using two ListViews, you use an ExpandableListView. Two ListViews on the UI might not lead to a good experience.
There's details here on how to deal with vast amounts of data.
An example here should help you with implementation of an ExpandableListView
Using 2 listviews in 1 activity is not recommended as it can cause you flows issues (Like the one you are facing right now).
Consider using only 1 listview, if there's different items override the following function (of BaseAdapter) to show correct views:
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
#Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
This will solve the 2 listview issue, than, when clicking on item and opening new activity you can save the an instance state of the list view so when you press back you can use the following example to see how to return to previous state.
Good luck
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);
}
}
I am trying to learn how to receive a notification on a click on a radio button in a ListView but nothing comes back when I click on a radio button next to a ListView item. Below is the code where I set up the listener. I am doing this in an asynchronous task under the onPostExecute() method where I populate my ListView from the server and my main activity extends MapActivity. Does anyone know what I am doing wrong?
protected void onPostExecute(ArrayList<String> result) {
// ... some code
mapView.postInvalidate();
final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_list_item_single_choice,
viewline);
ListView restaurant_list = (ListView) findViewById(R.id.list);
restaurant_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
restaurant_list.setAdapter(arrayAdpt);
restaurant_list.setScrollContainer(true);
restaurant_list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.e("listargs", (String.valueOf(arg1)) + " " + String.valueOf(arg3));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Does anyone know what I am doing wrong?
I recommend to you use OnItemClickListener instead of OnItemSelectedListener
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View parent,
int position, long id) {
// do your stuff
}
});
OnItemClickListener is usually used when you want to catch click events. OnItemSelectedListener is on the other side usually used with Spinner.
It sounds like you might have focus problem. Click listener only works if no other view can take focus.
Alter your checkbox xml with the following:
focusable="false"
Or set it to false if you are generating your layouts in code.
OnItemSelectedListener is for trackpad/trackball events
OnItemClickListener is for click events
Personaly I always implement both for safety.
I'm just learning how to use ListViews. I got it working, but wont to be able to respond when some one clicks a item.
I'm trying to use the setOnItemClickListener method to take a call back for when a item is clicked on. But my code will not compile due to errors in method setOnItemClickListener
r
Right now i get a error that says
setOnItemClickListener is not applicable for arguments OnItemClickListener();
void SetUpList()
{
listView = (ListView) findViewById(R.id.mylist);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile"};
EventsAdapter adapter = new EventsAdapter(this, cGlobals.eventsTitle);
// Assign adapter to ListView
listView.setAdapter(adapter);
// this is whare I get the error listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
}
});
}
}
First make sure you have imported this class:
import android.widget.AdapterView.OnItemSelectedListener;
Next you need to call setOnItemClickListener() like so:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override // "#Override" is required for Java 1.6, but forbidden in 1.5
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do Something
}
});
Or if your activity implements OnItemClickListener: You need to add the onItemClick() method outside your onCreate() method:
#Override
public void onCreate(Bundle savedInstanceState) {
// Do Something
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do something else
}
(Of course, if you are extending a ListActivity or ListFragment you should override onListItemClick() instead of onItemClick() like the second approach.)
I have a requirement that, in my list view I am having an image icon, a text view and a button.
when I click on any one of these it should move me to next screen. I tried implementing these methods, but no success.
Can anybody help me?
Note:My class does not extends ListActivity rather only Activity.
Here is my code....
ListView listView = (ListView)findViewById(R.id.listView1);
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Intent in2 = new Intent(City.this, TourDescription.class);
startActivity(in2);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
You need to set OnItemClickListener, not OnItemSelectedListener