在android中现有listview的顶部追加元素
我在活动中添加了listview,我想在其顶部追加数据。当活动加载时,会填充listview。现在,当用户单击按钮时,我会带来附加数据,但我想将此数据追加到listview顶部。我如何才能做到这一点 我使用在android中现有listview的顶部追加元素,android,Android,我在活动中添加了listview,我想在其顶部追加数据。当活动加载时,会填充listview。现在,当用户单击按钮时,我会带来附加数据,但我想将此数据追加到listview顶部。我如何才能做到这一点 我使用baseAdapter创建了自定义列表视图。下面是我的baseAdapter类: public class LazyAdapterUserAdminChats extends BaseAdapter{ private Activity activity; private ArrayList&
baseAdapter
创建了自定义列表视图。下面是我的baseAdapter类:
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;
}
}
公共类LazyAdapterUserAdminChats扩展BaseAdapter{
私人活动;
私有arraylisthashmap;
专用静态充气机=空;
公共LazyAdapterUserAdminChats(活动、ArrayList哈希映射)
{
这个。活动=活动;
this.hashmap=hashMaps;
LazyAdapterUserAdminChats.inflater=(LayoutInflater)this.activity.getSystemService(Context.LAYOUT\u inflater\u服务);
}
@凌驾
public int getCount(){
返回hashmap.size();
}
@凌驾
公共对象getItem(int位置){
返回位置;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=转换视图;
if(convertView==null)
视图=充气机。充气(R.layout.useradminchat,null);
TextView用户名=(TextView)view.findViewById(R.id.UAC_userNametext);
TextView messagetext=(TextView)view.findViewById(R.id.UAC_messagetext);
TextView messageDate=(TextView)view.findViewById(R.id.UAC_日期);
HashMap map=HashMap.get(位置);
username.setText(map.get(HandleJSON.Key_username));
setText(map.get(HandleJSON.Key_messagetext));
messageDate.setText(map.get(HandleJSON.Key_messageDate));
返回视图;
}
}
下面是如何从活动中为listview函数设置适配器
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();
}
}
private void ShowListView(ArrayList聊天)
{
尝试
{
ListView lv=(ListView)findViewById(android.R.id.list);
adapter=新的LazyAdapterLatestChats(此,chat);
低压设置适配器(适配器);
}
捕获(例外e)
{
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG.show();
}
}
首先,不要使用hashmap保存数据。您更愿意使用ArrayList,因为您将进行迭代。Hashmaps通常用于快速信息检索,通常不用于迭代(不过,这可以通过迭代器来实现)
接下来,在LazyAdapterUserAdminChats
上创建一个方法,将内容添加到arraylist的头部
最后,当添加到arraylist的头部时,调用notifyDataSetChanged
例如:
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();
}
}
但是如何在listview的顶部追加。谢谢你的adivce。使用你自己对象的arraylist。为什么需要hashmaps的arraylist?您对java相当陌生吗?如果要添加到arraylist的头部,只需调用alObj.add(object,index);在这种情况下,索引将是0.oh thnx,因为示例解决了它。那么您最喜欢哪种方法来实现listview
迭代器呢?
public class MyObj
{
String hashMapKey, hashMapValue;
}