Reputation: 3504
I've listview in an activity and I want to append data at the top of it.When the activity loads the listview is populated.Now when the user clicks a button i am bringing additional data but i want this data to append at the top of the listview.How can I accomplish this?
I've the custom listview made using the baseAdapter
.Heres my baseAdapter class:
public class LazyAdapterUserAdminChats extends BaseAdapter{
private Activity activity;
private ArrayList<HashMap<String,String>> hashmap;
private static LayoutInflater inflater=null;
public LazyAdapterUserAdminChats(Activity activity,ArrayList<HashMap<String,String>> hashMaps)
{
this.activity=activity;
this.hashmap=hashMaps;
LazyAdapterUserAdminChats.inflater=(LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return hashmap.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if(convertView==null)
view=inflater.inflate(R.layout.useradminchat,null);
TextView username=(TextView)view.findViewById(R.id.UAC_userNametext);
TextView messagetext=(TextView)view.findViewById(R.id.UAC_messagetext);
TextView messageDate=(TextView)view.findViewById(R.id.UAC_dates);
HashMap<String,String> map=hashmap.get(position);
username.setText(map.get(HandleJSON.Key_username));
messagetext.setText(map.get(HandleJSON.Key_messageText));
messageDate.setText(map.get(HandleJSON.Key_messageDate));
return view;
}
}
Here's how I set the adapter for listview function from my activity.
private void ShowListView(ArrayList<HashMap<String,String>> chat)
{
try
{
ListView lv=(ListView)findViewById(android.R.id.list);
adapter = new LazyAdapterLatestChats(this,chat);
lv.setAdapter(adapter);
}
catch(Exception e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Views: 640
Reputation: 9182
First of all, don't use a hashmap to hold your data. You'd much rather use an ArrayList, because you're going to be iterating. Hashmaps are usually used for fast information retrieval, and usually not for iteration (this can be done, though, with an Iterator
).
Next, create a method on LazyAdapterUserAdminChats
to add things to the head of your arraylist.
Lastly, call notifyDataSetChanged
when you add to the head of the arraylist.
Example:
public class LazyAdapterUserAdminChats extends BaseAdapter{
private Activity activity;
private ArrayList<MyObj> al;
private static LayoutInflater inflater=null;
public LazyAdapterUserAdminChats(Activity activity,ArrayList<MyObj> al)
{
this.activity=activity;
this.al=al;
LazyAdapterUserAdminChats.inflater=(LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// other methods
....
public void addToHead(MyObj m)
{
this.al.add(m, 0);
notifyDataSetChanged();
}
}
Your custom class can be anything you want. e.g.,
public class MyObj
{
String hashMapKey, hashMapValue;
}
Upvotes: 2