2012-07-15 86 views
0

现有列表视图的顶部我已经在一个活动列表视图,我想追加数据在它的顶部。当活动加载列表视图被填充。现在当用户点击一个按钮我我带来了额外的数据,但我想要这些数据追加在listview的顶部。我该如何做到这一点?追加元素在Android

我已经使用baseAdapter .Heres我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; 
} 

} 

下面是如何设置列表视图功能适配器从我的活动。

  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(); 
      } 
     } 

回答

2

首先,不要使用散列图来保存您的数据。你宁愿使用ArrayList,因为你将会迭代。散列表通常用于快速信息检索,通常不用于迭代(但可以通过Iterator来完成)。

接下来,在LazyAdapterUserAdminChats上创建一个方法,将其添加到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(); 
} 

} 

您的自定义类可以是任何你想要的。例如,

public class MyObj 
{ 
    String hashMapKey, hashMapValue; 
} 
+0

但是如何追加到列表视图的顶部。感谢adivce。 – Mj1992 2012-07-15 19:07:25

+0

你能通过代码示例来解释我吗? – Mj1992 2012-07-15 19:09:48

+0

使用您自己的对象的ArrayList。你为什么需要hashmaps的数组列表?你相当新的Java?如果你想添加到数组列表的头部,只需调用alObj.add(object,index); 在这种情况下,索引将会是0. – 2012-07-15 19:09:52