I have a custom ArrayAdapter which I use to populate a ListView in an Activity. The logic in the getView method of my ArrayAdapter requires an additional piece of information which is held in an instance variable in the host Activity. What's the best way of accessing this variable from within the getView method of my ArrayAdapter?
public class CustomArrayAdapter extends ArrayAdapter<String>
{
String addInfo;
public CustomArrayAdapter(String additionalInfo)
{
addInfo=additionalInfo;
}
}
when creating object for arrayadapter, use it like below
CustomArrayAdapter custAdapter=new CustomArrayAdapter("Additional Info");
Related
hello I need to refresh my List view From Adapter class or another activity.Please reply as soon as possible.
You can do it by just creating a static reference of your activity where you puted listview and calling the referesh function, after that just call that function in Adapter or any other class by just taking reference of that static variable of that class.
For eg: Classname.stataticvar.fucntion
Try it out.
Simply do this
//declare a static variable
public static MyListAdapter adapter;
//at the place where you call the adapter constructor;
MyListAdapter adapter=new MyListAdapter(//parameters here);
MyListAdapter=adapter;
//call from anywhere
try{
ClassName.adapter.notifyDatasetChanged();
}catch(Throwable e){
//error occured. Probably null
}
Just call notifyDataSetChanged() on your Adapter whenever the corresponding set of data is modified.
So far, a common pattern that I noticed in implementing an Array Adapter is to pass the layout twice to the adapter (in the constructor and in the getView) like this:
class MyAdapter extends ArrayAdapter{
MyAdapter(){
super(cxt, R.layout.adapter_layout);
}
getView(...){
convertView = inflate(R.layout.adapter_layout, ...);
}
}
But is it really necessary?
I am curious to know this as I am not able to find it in internet.
I am creating a custom list adapter. In this adapter there is a getView method which I am overriding. In this method I inflate the row layout which I pick from layout folder.
In this case I should not need to pass the same row layout as a constructor to the adapter, which is not allowed.
Please answer.
Regards
Utsav.
you can safely ignore it by overriding the constructor, like this:
public class MyAdapter extends ArrayAdapter {
public MyAdapter(Context context){
super(context, 0);
}
}
edit:
That exists for cases that you're not creating a custom adapter, e.g. new ArrayAdapter(context, R.layout.item);
This adapter would write the value of to string() of its data items to TextView with ID android.R.id.text1 inside that layout.
I have a ListActivity that is currently displaying a list of type Data objects. These type Data objects have custom data:
String title;
The following is in my ListActivity constructor:
List<Data> values = dataInterface.getAllData();
ArrayAdapter<Data> adapter = new ArrayAdapter<Data>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
The application displays on screen all the Data objects without error.
The problem is that the text displayed for each list item is:
Data#42731008
Data#427362c0
and so on.
I understand these are the object id's of the Data objects. Instead, I want to display the Strings found in Data.title. I can't figure out how to accomplish this.
If you don't want to use a custom Adapter, then define the public String toString() method in your Data class as follows:
public String toString {
return title;
}
It will be used by the ArrayAdapter when it represents the object in the ListView.
In your adapter :
holder.myTextview.setText(getItem(position).getTitle());
The point is in the getItem(position).getTitle(), because your custom adapter extends ArrayAdapter<Data>, getItem(position) will return an instance of Data.
After that, you just need to get the title from the Data using getTitle(); (depends on your setter - getter in you Data class).
Just comment if you dont understand something or if i miss understand you :)
I have an application developed to get all the third party applications installed and to display them on a list view! this is been done by an extended baseAdapter. In this list I do an uninstallation of a selected application using this code :
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + Packname));
startActivity(intent);
Now what I want is to update the list view with the changed data so that the user can get updated application list. how can I achieve this. I found that notifyDatasetchanged method but it can only be used for simple listviews! what are the options that I have and please let me know of any tutorials to achieve my outcome!
thank you.
What do you mean only for simple listviews? How is yours different? You should be able to call notifyDataSetChanged on your custom adapter class after deleting one of your items. You can create a function to do it...
private ArrayList<Object> list;
public void deleteItem(int index){
list.remove(index);
notifyDataSetChanged();
}
you will have an adapter (something that extends BaseAdapter probably) that you set into the ListView using setAdapter(...). add a method to your adapter implementation class that sets the underlying objects that are backing the list. make sure to call notifyDataSetChanged() at the end. something like,
public class MyAdapter extends BaseAdapter<String> {
private List<String> strings;
...
void setModel(List<String> strings) {
this.strings = strings;
this.notifyDataSetChanged();
}
}
simply calling your adapter's setModel(...) will cause the ListView to update.