How to handle 2 Listvews in one activity Android - android

I have created 2 listviews in 1 Activity.
Eg:Vegetables
Fruits
Meat
So when you click on Fruits I want all the 5 Fruits available to show in the 2nd listView. Can you please tell me how to do this? My code is given below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listv);
ListView menu= (ListView) findViewById(R.id.listView1);
String items[]= {"Fruits","Vegetables","Jooses","Meat","Toys","Cookeys"};
ListView menu2= (ListView) findViewById(R.id.listView2);
String subitems[]= {"xxx","xxx","xxx","xxx","xxx","xxx"};
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems));
}

Set the adapter for the second listview in the SetOnItemClick adapter of first listview:
menu.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, check which item is it and then set adapter and display values in 2nd listview
}
});
Refer:
http://matrix-examplecode.blogspot.in/2011/11/listview-example.html
http://www.mkyong.com/android/android-listview-example/
http://developer.android.com/reference/android/widget/AdapterView.html

## -----lets try this simple code. this will suites your need-----##
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listv);
ListView menu= (ListView) findViewById(R.id.listView1);
ListView menu2= (ListView) findViewById(R.id.listView2);
String items[]= {"Fruits","Vegetables","Jooses","Meat","Toys","Cookeys"};
String subitems1[]= {"xxx1","xxx","xxx","xxx","xxx","xxx"};
String subitems2[]= {"xxx2","xxx","xxx","xxx","xxx","xxx"};
String subitems3[]= {"xxx3","xxx","xxx","xxx","xxx","xxx"};
String subitems4[]= {"xxx4","xxx","xxx","xxx","xxx","xxx"};
String subitems5[]= {"xxx5","xxx","xxx","xxx","xxx","xxx"};
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
// on item click listener
menu.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// get the selected values from listview 1
String val = items[arg2];
if(val.equalsIgnoreCase("Fruits"))
{
// set the second listview 2
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems1));
}
else if(val.equalsIgnoreCase("Vegetables"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems2));
}
else if(val.equalsIgnoreCase("Jooses"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems3));
}
else if(val.equalsIgnoreCase("Meat"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems4));
}
else if(val.equalsIgnoreCase("Toys"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems5));
}
}
});
}

Register an onItemClickListener callback on menu (Listview).
menu.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick (AdapterView<?> parent, View view, int position,
long id)
{
/* TODO: Update menu2 adapter items */
/* Position - determines the item that is being clicked on your menu listview,
by that. you can render your second adapter based on the item selected */
/* Call adapter notifyDatasetChanged method to inform adapter that its data set
has changed. */
menu2Adapter.notifyDatasetChanged();
}
});
Or other design may be. Put your 2nd listview on a second activity. every time you
select an item. Push the new activity that points to the 2nd list view..

when you click an item in the left listview, the OnItemCLickListener will be triggered. In the callback, update the underlying data maintained by the second listview and call notifyDataSetChanged.

Check the following answer...I checked this code by myself. May be what you want to do.
ListView menu = (ListView) findViewById(R.id.listView1);
String items[] = { "Fruits", "Vegetables", "Jooses", "Meat", "Toys", "Cookeys" };
final ListView menu2 = (ListView) findViewById(R.id.listView2);
final String subitems[] = { "xxx", "xxx", "xxx", "xxx", "xxx", "xxx" };
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//Here YourActivity.this means the Context
menu2.setAdapter(new ArrayAdapter<String>(YourActivity.this,
android.R.layout.simple_list_item_1, subitems));
}
});

Override your first listview(menu in your example) onItemClickListener
and prepare subitems (list to display in menu2) according to your need
or whatever you wants to display and call notifyDataSetChanged() on
menu2 adapter ( menu2.getAdapter().notifyDataSetChanged(); ).

Related

I want to display selected item with some title in Spinner,Like item is:item6

I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

ListView onClick update clears the list

I have the following code that's supposed to remove the item that is clicked on:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String itemValue = (String) listView.getItemAtPosition(position);
email_addresses.remove(position);
String size = Integer.toString(email_addresses.size());
Log.d("Size: ", size);
//listView = (ListView) findViewById(R.id.email_list);
//ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, email_addresses);
//listView.setAdapter(adapter);
ArrayAdapter<String> adapter = new ArrayAdapter<>(mainAppContext, android.R.layout.simple_list_item_1, android.R.id.text1, email_addresses);
((ListView)parent).setAdapter(adapter);
parent.refreshDrawableState();
}
When I click on an item the list is cleared and nothing is displayed. The size is the correct value and the list can be rebuilt but I am unable to get this code to work.
Thank you
Instead of creating a whole new Adapter each time, have you thought about simply making your Adapter a member variable and calling notifyDataSetChanged?
ArrayAdapter<String> adapter = new ArrayAdapter<>(mainAppContext, android.R.layout.simple_list_item_1, android.R.id.text1, email_addresses);
((ListView)parent).setAdapter(adapter);
Becomes:
mAdapter.notifyDataSetChanged();
Define your list and adapter as instant variables and also You have to notify the adapter in order to make the deletion affect ,
Try this ,
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
email_addresses.remove(position);
adapter .notifyDataSetChanged();
}
});

Display Data in Spinner,Android

I wanted to display my data into android spinner from database.
I used two spinner..
2nd spinner should reflect once 1st spinner item is selected, every thing is working fine..data is loaded into the second spinner, but not displaying into 2nd spinner when 2nd spinner item is selected.
Spinner1 = (Spinner)findViewById(R.id.createProfileCitySpinnerId);
Spinner2 = (Spinner)findViewById(R.id.createProfileStateSpinnerId);
//for 1st spinner.....(working)
final List<String> list1 = new ArrayList<String>();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,list1);
Spinner1.setAdapter(adapter1);
//for second spinner...
final List<String> list2 = new ArrayList<String>();
Spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
//everything is working data is loading, but not display once item is selected on 2nd spinner
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list2);
stateSpinner.setAdapter(adapter2);
You need to add the element directly to the adapter (not to list2), the ArrayAdapter keeps its own internal data. Try this code on your onItemSelected:
adapter2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();
Call adapter2.notifyDataSetChanged();
such as
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();
}
}
see http://developer.android.com/reference/android/widget/ArrayAdapter.html for more information.
public void notifyDataSetChanged () Since: API Level 1
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.

Android: Cannot create a list with some items expandable and some items not

How can I create a list with some items expandable and some items like a normal listView. I have got a normal listView working but cannot integrate expandable list items.
Here is what I have so far
public class Home extends ListActivity{
String[] items={"John", "Tom", "William", "Jake"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.home, items));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
Any help would be really appreciated.
Thanks in advance.
You're going to need override the getViewTypeCount() and getItemViewType() methods. This means your going to have to write your own Adapter class.

How do I enable clicking in ListView

I want to get the text element that makes up my ListView items.
myList = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, partialNames);
myList.setAdapter(adapter);
I am not extending ListActivity so I can't call a simple onListItemClick function.
I have tried using the following:
myList.setOnItemClickListener(adapter.?)
, but I don't know what parameters I need for the constructor
How can I capture the string value in a particular ListView item when the user clicks on the row?
Make your Activity implement OnItemClickListener, and then:
myList.setOnItemClickListener(this);
or if you dont want to implements OnItemClickListener, you can do:
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
// TODO Auto-generated method stub
}
});

Categories

Resources