OnFinishInflate inside ListActivity - android

I have a main class filled with a cursor adapter:
public class MainMenu extends ListActivity{
...
private void updateData(){
...
SimpleAdapter notes = new SimpleAdapter(this, array, R.layout.row_task, from, to); setListAdapter(notes);
}
}
I need to know when this list has finnished to inflate it, so I've discovered this methods:
ListView.onFinishInflate()...
But I have no idea how to override it inside my code.
Any idea?

You'll need your own View that will extend ListView. And than you can override whatever you want.
Note: Method is not specific to the ListView it's inherited from View. But since you'll need functionality of the ListView you'll need to extend ListView.

Related

Why do we need to pass a resource layout to a custom adapter in constructor?

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.

Android ListView ArrayAdapter getting data from host activity?

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");

Change color of specific listview item

I need to change the text color of some specific items of the listview.
lstdetails = (ListView) findViewById(R.id.lstdetails);
ListAdapter adapter = new SimpleAdapter(
Details.this, details_list,
R.layout.Details, new String[] {
"lecture_key", "date",
"total_lect_int", "attendance" },
new int[] { R.id.tvKey, R.id.tvdate,
R.id.tvtotallect, R.id.tvattendance });
lstdetails.setAdapter(adapter);
Text color needs to be changed on the basis of the value of R.id.tvattendance
The above snippet is in the onPostExecute function of the class extending AsyncTask
You need to implement custom adapter, extend from BaseAdapter class, and change color in getView method
You need to implement custom adapter
You can view the full implementation i did in this opensource project
Customadapter in Fragment
private class listviewAdapter extends BaseAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int value= listAdapter.get(position); // or whatever data type is your value
if (value==0){
//change color of the textview at runtime here
}else{
//or some other color
}
}
}
i am using this inside a fragment and it works perfectly
I think there is two option for you to accomplish it.
Normal Way : Using a custom adapter and customize each row as you wish. For this way, please take a look at here
Ugly Way! : Use mListView.getFirstVisiblePosition() and mListView.getLastVisiblePosition() as well as mListView.getChildAt(...) to obtain a reference to that row and then customize it as you wish.

How can i hide a listview item in android?

Hi friends i have a listview and the contents are fetched from a webservice call. In that webservice call, there are fields like
"OGType": "ORG" and "OGType": "GROUP"
If click a button, the listview must shows the item having "OGType": "ORG", and hide the item having "OGType": "GROUP". Hope you understand what i meant. Please anyone help me for that. Thanks in Advance.
Try to set new data (only with ORG) to adapter and then call
adapter.notifyDataSetChanged();
You can do it in your getView Method in your Adapter Class. That's the header
public View getView(int pos, View convertView, ViewGroup, parent)
There you can properly hide the element(s) you want, you know, using the method setVisibility()
For more help you can take a look here
You can create a custom adapter and pass data to it in the form of Array or ArrayList (ArrayList is better when dealing with Custom Adapters). Whenever you need to add or remove the data from ListView, just add or remove the item to or from you ArrayList and call notifyDataSetChanged() on your custom adapter and it will update the ListView automatically.
In your case, whenever you click a button, edit you ArrayList and call your custom adapter's method called notifyDataSetChanged() and that's it. You'll see every time you call this method ListView will refresh itself if you have made any changes to the data. Hope it helps.
NOTE - CUSTOM ADAPTER IS NOT COMPULSORY. ANY ADAPTER CAN BE USED e.g SimpleAdapter, ArrayAdapter etc.
You can use a visible list and filters lists. You should use "visible" for complete the BaseAdpter as always, then, you can change the pointer of visible to other list (all, filter...)
Don't worry by the memory, are pointers, you only have each element only once.
public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> visible;
private ArrayList<MyItem> all;
private ArrayList<MyItem> filter;
public MyAdapter(ArrayList<MyItem> items) {
all = items;
visible = all; //Set all as visible
filter = new ArrayList<Item>();
for (Item i : items)
if (i.getType().equals("ORG"))
filter.add(i);
}
//Complete adapter using "visible"
public void showOnlyOrg() {
visible = filter;
notifydatasetchanged();
}
}
The non hackish way will be to remove the items from your Collection which you use to generate the listview and then call notifyDataSetChanged();

Cannot extending both ListActivity and AsyncTask

Since java does not support multiple inheritance I have a problem getting this to work.
This is what I want to do :
public class CallForwardActivity extends ListActivity /* This is there I want to extend AsyncTask */ implements OnSharedPreferenceChangeListener
{
... // creating and initializing stuff
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
userInfo = this.getSharedPreferences(USERINFO_FILE, 0);
userInfo.registerOnSharedPreferenceChangeListener(this);
userControl = new UserController(context);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
//setListAdapter(new ArrayAdapter<String>(this, R.layout.callforward_items, R.id.callforward_item_text, callforwardLabels));
list = new ArrayList<HashMap<String,String>>();
callForwardList = new SimpleAdapter(
this,
list,
R.layout.callforward_items,
new String[] { "line1","line2" },
new int[] { R.id.callforward_item_text, R.id.callforward_number } );
populateList();
setListAdapter( callForwardList );
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
CustomDialog(); // this will make the asynchronous task call if the user presses "OK" within the dialog box.
}
});
}
How do I go about making the call asynchronous without extending AsyncTask ?
Just create another class that extends AsyncTask and use it in your ListActivity. You can make it inner if you like. Hope this helps.
errrmmm.... there's no way you can extend two classes in java you can implement many interfaces but extend one class. you should create another say inner class which extends AsyncTask<..,..,..> and do your background jobs there.
More on multiple inheritance in java
http://java.sys-con.com/node/37748
What is use case you are trying to use activity extending a asynctask, asynctask is used for background process, Acitivity component is used to provide UI
You are trying UIThread extends BackgroundThread, which means you are making a relationship like 'UIThread is-a BackgroundThread'.
I feel there is another way to solve your use case.

Categories

Resources