How can i change image from adapter.
I have created a ListView and in this listview i have added dynamic item which has name and picture. I want to change the pic from that item on some logic.
adapter is created by below code
SimpleAdapter adapter = new SimpleAdapter(this, menuItems , R.layout.list_item, new String[] { NAME, EMPID }, new int[] { R.id.name, R.id.empid });
setListAdapter(adapter);
ListView lv = getListView();
below Code works fine if under OnItemClickListener. but i want image in at the time of rendering first view.
lv.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(XMLUtil.getNodeValue(group, "Gender").equalsIgnoreCase("male")){
view.findViewById(R.id.imageView).setBackgroundResource(R.drawable.male);
}else{
view.findViewById(R.id.imageView).setBackgroundResource(R.drawable.female);
}
}
Set new image in menuItems and call adapter.notifyDataSetChanged() for updating your ListView
Related
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(); ).
How can I insert an OnClickListener for list view? I want to show an alert dialog with two buttons, "yes" and "no", when I click an item.
private void loadListViewData() {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<String> lables = db.getAllLabels();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line,
lables);
listview.setAdapter(dataAdapter);
}
If you want to set Click Listener on item, then use setOnItemClickListener, on listview, using this method you will have access to clicked item position too, simply do as below:
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView adapterView, View convertView, int position, long id)
{
//Do some operation
}
});
I am having an issue with trying to retrieve the contents of the view that the the user has clicked within a ListView. My ListView is setup using a SimpleAdapter and is comprised of a "heading" and a "sub-title" for each of the items in the list. These are stored using a HashMap.
Within the activity also, is a spinner, when the user selects an item in the spinner, the ListView is updated. This is working fine and I just thought it necessary to mention what is happening within the activity as well.
What I am trying to do, is retrieve which item the user has selected so I can guide them to a new activity based on their selection (right now just trying to display the view contents with Toast). I want to retrieve the contents of what is stored under the "engExp" key for the item.
Here is my code:
// HASHMAP FOR LISTVIEW
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int x = 0; x < expressionListForModule.getCount(); x++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("engExp", expressionListForModule.getString(0));
map.put("japDef", expressionListForModule.getString(1));
fillMaps.add(map);
expressionListForModule.moveToNext();
}
// SETUP LISTVIEW
SimpleAdapter adapter2 = new SimpleAdapter(this, fillMaps, android.R.layout.simple_list_item_2, new String[] {"engExp","japDef"}, new int[] {android.R.id.text1, android.R.id.text2});
ListView lv = (ListView) findViewById(R.id.expressionList);
lv.setAdapter(adapter2);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_LONG).show();
}
});
Specifically, the item that is causing the application to crash is this:
((TextView) view).getText()
If you need to see any more of my code, please let me know.
Any help is appreciated, thanks in advance.
try
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView v = (TextView) view.findViewById(android.R.id.text1);
String contentForLink = v.getText().toString();
Toast.makeText(getApplicationContext(), contentForLink, Toast.LENGTH_LONG).show();
}
});
I have a ListActivity in which i have used a SimpleAdapter to create a list which has 2 fields. The pair of values are stored using a Map, and the list is an ArrayList.
I have an onItemClickListener for this. On selecting a list entry, i get the pair i.e. the 2 values. I need to get only 1 of those values.
For example, if the list item is "John", "123" . I want to store "123" in a string after selecting the entry from the list
Any help?
Here's the code snippet:
ListView lv = getListView();
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData(scanned_name, scanned_addr));
String[] from = { "name", "address" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(getApplicationContext(),
parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
});
You have to catch the content of android.R.id.text1 or android.R.id.text2. So, in your onItemClick(...) you have to do:
TextView v = (TextView)findViewById(R.id.text1);
TextView v1 = (TextView)findViewById(R.id.text2);
String content1=v.getText().toString();
String content2=v2.getText().toString();
and you can manage them as you want, even within a Toast:
Toast.makeText(getApplicationContext(),v.getText().toString(),Toast.LENGTH_LONG).show();