When I click a spinner item,I would like list view to shows more details on that item.
i.e if I select Student from Spinner then list view should show student names.
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(SpinnerActivity.this, parent.getItemAtPosition(position)+" ", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
What is the right way to do it ?
Create an ArrayList with Strings(Student names). Now create an ArrayAdapter in onCreate() which contains the Students ArrayList.
In the EventListener(onItemSelected) of the Spinner, check for the Spinner-item text. If it's "Students" set the ArrayAdapter of the Spinner to the Students-ArrayAdapter which you initialized in onCreate();
onCreate():
arrayList = new ArrayList<String>();
arrayList.add("Bob");
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
onItemSelected EventListener:
if(view.getText().toString().equals("Students"){
listView.setAdapter(adapter);
}
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();
}
});
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(); ).
I used list view to list the items of my SQLite database as follows:
listView = (ListView) findViewById(R.id.listView1);
db.open();
Cursor c = db.getAllHistory();
ArrayList<String> temparr = new ArrayList<String>();
if (c.moveToFirst()) {
do {
temparr.add(c.getString(0)+'\t'+c.getString(1)+'\t'+c.getString(2)+'\t'+c.getString(3)+'\t'+c.getString(4)+'\t');
} while(c.moveToNext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, temparr);
listView.setAdapter(adapter);
} else {
db.close();
}
Now I want to access the specific list item by using OnItemClickListener. How should i do it?
To get your perticular data-
Implement setOnItemClickListener()-
and position will return list's item position number:-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String yourData = temparr.get(position);
}
});
Just call listView.setOnItemClickListener() with your implementation of the listener.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// your code
// Toast.makeText(context,temparr.get(position),Toast.LENGTH_SHORT).show();
}
});
parent -> The AdapterView where the click happened.
view -> The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position -> The position of the view in the adapter.
id->The row id of the item that was clicked.
You can set the listener like this:
Put this code in onCreate()
TextView disp = (TextView) findViewById(R.id.textView1);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String temp = (String) listView.getItemAtPosition(position);
disp.setText(temp);// display on screen
}
});
EDIT:
As you want to display the listItem on screen in a permenant sort of way rather than displaying a toast, the easiest way that you can do this is by adding a TextView in your layout. See the updated code
Hope this helps.
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.