2 ListViews in the same activity - android

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.

Related

onItemClick is conflict with onItemClickListener of SwipeListViewCallback

i have used the following :
public class CategoryFragment extends Fragment implements SwipeListViewCallback {
private ListView category_linear;
//code...
category_linear.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent ii = new Intent(context, otherscreen.class);
startActivity(ii);
}
});
and i have also the method of SwipeListViewCallback:
#Override
public void onItemClickListener(ListAdapter adapter, int position) {
// TODO Auto-generated method stub
}
when i click once on listview item then it is not open other activity.
When click twice then and then it is working.
I want to open other activity on listview item on one click
Solution:
I have removed the `onItemClickListener` from SwipeListView class.
and also removed the method of swipe listview.
#Override
public void onItemClickListener(ListAdapter adapter, int position) {
}
So when i click on the item from list view then it is called the list view on click listener.

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

how can i call from onItemClick to onListItemClick method in android

I have a button and a list view. I am setting three adapter in one listview as per click on list but when I press back button it will click only one time.
The code is here:
public class GamesFragment extends ListFragment{
Context context;
Dialog dialog;
Game_Adapter gameadapter;
Game_Channels_Sections_Adapter GameChannelsSections;
Game_Sections_Details_Adapter GameSectionsDetails;
ListView listView1;
JSONArray dataJsonObject;
Button back,back2;
TextView title;
Button add;
int i=0;
int y=0;
int z=0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.game_list, container,false);
listView1=(ListView)view.findViewById(android.R.id.list);
back=(Button) view.findViewById(R.id.button_back);
GamesFragmentData dataobserver=new GamesFragmentData();
add=(Button) view.findViewById(R.id.button_add);
title=(TextView) view.findViewById(R.id.textview_caption);
title.setText("Games");
add.setVisibility(View.INVISIBLE);
ServerManager.getInstance().addObserver(dataobserver);
ServerManager.getInstance().readLoopForGameData(AppConstances.Games_Channels);
if(gameadapter==null)
gameadapter=new Game_Adapter(getActivity(), new JSONArray());
listView1.setAdapter(gameadapter);
Log.e("","In ongameadapter---->");
return view;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
System.out.println("In onListItemClick---->");
Log.e("","In onListItemClick---->");
ServerManager.getInstance().readLoopForGame_Channels_Sections_Data(AppConstances.Games_Channels_Sections);
GameChannelsSections=new Game_Channels_Sections_Adapter(getActivity(),new JSONArray());
listView1.setAdapter(GameChannelsSections);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.e("","In onListItemClick1---->");
listView1.setAdapter(gameadapter);
}
});
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
ServerManager.getInstance().readLoopForGame_Channels_Sections_Details_Data(AppConstances.Games_Sections_Details);
GameSectionsDetails=new Game_Sections_Details_Adapter(getActivity(),new JSONArray());
listView1.setAdapter(GameSectionsDetails);
}
Log.e("","In onItemClick---->");
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.e("","In onItemClick1---->");
listView1.setAdapter(GameChannelsSections);
}
});
}
});
super.onListItemClick(l, v, position, id);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
The code has two method:
onListItemClick
onItemClick
When I click on list view control, it goes from onListItemClick to onItemClick because I am setting three adapter in single list view on onclick.
But when I click back button, I need to take back. I cannot go back.
Say, if I press on adapter 1, I set adapter 2 AND if I press adapter 2, I have set adapter 3.
Now, I am at adapter 3. On click of back button, I go back from 3 to 2 but I cannot go from 2 to 1.
In short how can I go control back from onItemClick to onListItemClick so that the flow of clicking can be repeated?

Propagate button onclick of custom arrayadapter in my activity

My activity has a ListView that has a custom ArrayAdapter.
On my ArrayAdapter i have an image, a bunch of textboxes and a button.
On the getView of the adapter i get my button and set setOnClickListener. From the click listener i can get the index of the clicked item.
Now my problem is that i want to propagate that information to my main activity, where i want to handle my button click.
I can save the index information in a static var, but i still don't know how to fire an event in my activity.
How do i do that?
I'm 6 days new to Android so, thanks
iggy
Code:
My Activity:
public class MyClass extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
...
myListView = (ListView)findViewById(R.id.lvxml);
myList = new MyCustomArrayAdapter(this, myAnotherClassObject);
myListView .setAdapter(myList);
...
}
}
Now in my Adapter
public class MyCustomArrayAdapter extends ArrayAdapter<myAnotherClass> {
...
....
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
doStuff;
}
});
}
}
I need to somehow fire a buttonclick from my main activity, but without loosing the possibility to read the index clicked in the list view.
Here's an alternative and, what I think to be, more elegant solution.
Firstly, in your MyCustomArrayAdapter class, define an interface:
public interface MyCustomRowButtonListener{
void onCustomRowButtonClick(MyAnotherClass selectedItem, int position, View view);
}
Create an member variable of MyCustomRowButtonListener in your ArrayAdapter
public class MyCustomArrayAdapter{
private MyCustomRowButtonListener mRowButtonListener;
//....
}
and add a parameter for the listener in the constructor
public MyCustomArrayAdapter(Context context, MyCustomRowButtonListener listener){
mContext = context;
mRowButtonListener = listener;
}
in the getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mRowButtonListener.onCustomRowButtonClick(getItem(position),position,b);
}
});
}
and in your activity:
myList = new MyCustomArrayAdapter(this, myAnotherClassObject,this);
Now let your activity implement MyCustomRowButtonListener
public class MyClass extends Activity implements MyCustomRowButtonListener{
...
public void onCreate(Bundle savedInstanceState) {
...
myListView = (ListView)findViewById(R.id.lvxml);
myList = new MyCustomArrayAdapter(this, myAnotherClassObject,this);
myListView .setAdapter(myList);
...
}
}
public void onCustomRowButtonClick(MyAnotherClass selectedItem, int position, View view){
Toast.makeText(this,"You have selected "+selectedItem,Toast.LENGTH_SHORT).show();
}
So I've solved it somehow. It's probably not the way to do it but it works.
I save my activity in a static var.
Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setTag(position);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int i = (Integer) v.getTag();
myStaticVarRep.myActivity.myMethod(i);
}
});
The index ist the position variable.
Ist has to be final to use it here
public View getView(final int position, View convertView, ViewGroup parent)
You can inform your Activity with a listener-interface.
I'm going to assume your code looks a little like this.
public class <Your Class> ... implements OnClickListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
.
button.setOnClickListener(this);
}
}
To handle button click events, you need to have an onClick method that looks like this.
public void onClick(View v){
<Handle your button click in here>
}
Whenever you click your button, it'll invoke the onClick method. That is where you would manipulate your static var, fire off other methods, etc. I hope that helps.

How do I select a row in a listview not using ListActivity

Here is the code:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find your views
final ListView listPro = (ListView) findViewById(R.id.listProperty);
DBAdapter db = new DBAdapter(this);
db.open();
final Cursor c = db.getAllProperties();
startManagingCursor(c);
// Create the adapter
MainAdapter adapter = new MainAdapter(this, c);
listPro.setAdapter(adapter);
// here is where I think I should put the code to select the row but I
// haven't been able to figure out how to do it.
// all I need to get out is the row id number.. please help
Button addPropBut = (Button) findViewById(R.id.mainButton);
addPropBut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i7 = new Intent(Main.this, Home.class);
startActivity(i7);
}
});
}
}
try this one, hope it will help you out.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
you can get the id number by using Log or System.out.println();
Instead of using Button's setOnClickListener use ListView's setOnItemClickListener.
listPro.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
The id parameter will give you the needed id.
Salil.

Categories

Resources