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
Related
I have a ListView with a simple ArrayAdapter
dogsList.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, dogsArray));
If a user clicks on an item, the background color changes. So far so great.
Here is my problem: when the user returns, I want to pre-highlight the last selection. I have tried a number of things to pre-highlight one of the entries but it’s not working.
Assume I know the previous selection and pass it through intent or whatever, say prev=5. How do I highLight the background?
I am using a real device (Note 5) to test. And I don't understand whether this applies or not: Android ListView programmatic selection/highlight. In any case, I did it and it didn't work: no highlight.
May be this will help
View row = null;
list_view.post(new Runnable() {
#Override
public void run() {
int nPos = Prev;
list_view.setSelection(nPos);
View v = list_view.getChildAt(nPos);
if (v != null) {
option_list.performItemClick(v, nPos, list_view.getItemIdAtPosition(nPos));
v.requestFocus();
}
}
});
list_view.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
// TODO Auto-generated method stub
if (row != null) {
row.setBackgroundResource(R.color.DarkGray);
}
row = v;
v.setBackgroundResource(R.color.White);
}
});
In my android app I have two List Views and two buttons.On clicking first button, first List View will be visible and on second button second List View will be visible.In one List View I will have list of items and in second List View there will be ADD button. Now clicking on ADD button my first List View will be visible and user can select items to add in second List View. I have done it successfully. But my problem is, by clicking on second button List View is appearing but there are no data which are added from First List View. Below is my code.
What am I missing?
final ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
final View header = getLayoutInflater().inflate(R.layout.footer, null);
lv.addFooterView(header, null, false);
btnCollege.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String listItems[] = {};
final ArrayAdapter<String> string = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1);
lv.setAdapter(string);
Button btnAdd = (Button) header.findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (j == 0)
{
lv.setVisibility(View.VISIBLE);
header.setVisibility(View.VISIBLE);
j = 1;
}
else if (j == 1)
{
lv.setVisibility(View.INVISIBLE);
header.setVisibility(View.INVISIBLE);
j = 0;
}
else {}
lv1.setVisibility(View.VISIBLE);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String selected = lv1.getItemAtPosition(arg2).toString();
string.add(selected);
string.notifyDataSetChanged();
setListAdapter(string);
}
});
}
});
}
});
The view is not getting updated. use handlers for updating listview. That would help you solve the issue
I am working on an android app, and I am having difficulties with ListActivity. I would like to have a different Activity start depending on which item in the list is clicked.
I made a list and referenced it with setListAdapter in java, but I am not sure how to reference in the OnListItemClick. I assume I need to reference the position in the list.
With Activity and Buttons, I can set the OnClickListener and use a switch statement with a case for each Button. What is the equivalent for ListActivity?
Here is my code:
public class Options extends ListActivity implements {
String myHistory[]= { "Item 1", "Item 2", "Item 3" };
//---Set ListView---
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String> ( Options.this,
android.R.layout.simple_list_item_1, myHistory)));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
//--if click position 1, do below
//--Intent a = new Intent("show Item 1's corresponding page");
//--startActivity(a);
//--if click item in position 2, do below
//--Intent b = new Intent("show Item 2's corresponding page");
//--startActivity(b);
//--if click item in position 3, do below
//--Intent c = new Intent("show Item 3's corresponding page");
//--startActivity(c);
}
}
I'm not sure there are other ways of doing it, but I do it this way:
I set the click listener on the activity class (not on the adapter one, which I think makes more sense).
SAve an array of the classes you wish to call:
Class[] classList = new Class[]{class1.class, class2.class....};
add the listener to the listview
lv.setOnItemClickListener(listviewClickListener());
Then the onItemClickListener method:
private OnItemClickListener listviewClickListener() {
OnItemClickListener clickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
//And then call the corresponing one
Intent I= new Intent(thisActivity, classList[i]);
int id = listOfObjects.get(position).getId();//do whatever you want with the object on the postition "position"
Bundle bundle = new Bundle();
bundle.putInt("id", id);
i.putExtras(bundle);
thisActivity.startActivity(i);
}
};
return clickListener;
}
I didn't test the array of class literals, but I guess it should work..
Edit: I'm considering you want to create different activities for each item (which thinking about it doesn't make much sense (not having the context) as all items on a list view belong to the same group, and therefor might probably be used in the same way). Otherwise you don't need the list of activities, and just add the one you wish to the intent.
as looks there are only few and certain data in list not at run time because each item has a separate activity (like a menu list of game) so I will suggest to go short and simple .......
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent a;
switch(position){
case 1:
a = new Intent("show Item 1's corresponding page");
break;
case 2:
a = new Intent("show Item 2's corresponding page");
break;
case 3:
a = new Intent("show Item 3's corresponding page");
break;
if(null!=a)
startActivity(a);
}
}
you must have to use Adapter object and set that with the ListView Object.
ListView listView= getListView();
Adapter medicineListAdapter = new ArrayAdapter<String>(
Options.this, android.R.layout.simple_list_item_1, myHistory);
listView.setAdapter(medicineListAdapter);
Add this and in Class implements onItemClickListener .This refer that onItemClick method.
onListItemClick(ListView l, View v, int position, long id)
position Corresponding your item of myHistory
you can judge position to do something :
switch (position) {
case 1:
Intent a = new Intent("show Item 1's corresponding page");
//--startActivity(a);
and so on...
I know, similar questions (ListView selected items are messed up when scroll the ListView...) are asked before and the answer is doing operation in onItemClick, not in getView.
My situation is a little bit complicated. I have a custom view in each ListView row. This custom view has 5 ImageViews. So I catch onClick event of these ImageViews and change drawable image. In this case, I don't know how to move this operation to onItemClick. Any ideas?
do like this. you can change the color or background of selected item on listview. use this code in getView() method.
m_Button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
indexClick = position;
indexClickFlag = true;
notifyDataSetChanged();
Log.i("in click--------->", "" + position);
Intent i = new Intent(ArtistPlaylistActivity.this,
PlayerMainActivity.class);
i.putExtra("songnameIndexinPlaylist", position);
startActivity(i);
ArtistPlaylistActivity.this.finish();
}
});
if (indexClickFlag) {
if ((indexClick) == (position)) {
itemLayout.setBackgroundResource(R.drawable.tab_select);
Log.d("System out", "iclick" + indexClick + "pos"
+ position);
}
}
where, int indexClick ; boolean indexClickFlag = false;
itemLayout -- > is layout that background that you want to change and m_Button.
Hope helpful to you.
You can get the Child from the view using getChildAt(index) method. put the index of image view to get the image view object
Sample Code
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println(".....view..."+arg1);
LinearLayout ll =(LinearLayout) arg1;
ImageView imageView = (ImageView) ll.getChildAt(0);
imageView.setImageDrawable(drawable);
}
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;
}
}
});
}