我正在创建聊天应用程序。所以我想增加用户从他的朋友那里得到的消息数量。为了表明我创建了自定义Array adapter
是因为我的列表视图由朋友姓名和notification textview
组成。
因此,我的list_of_registerd_users活动中包含数据:
如何将这些数据发送到自定义array adapter
类以设置通知视图:
自定义阵列适配器类:
public class CustomArrayAdapter extends ArrayAdapter<String> {
private Context mContext;
private int mRes;
private ArrayList<String> data;
private String numOfMsgs;
public CustomArrayAdapter(Context context, int resource,
ArrayList<String> objects) {
super(context, resource, objects);
this.mContext = context;
this.mRes = resource;
this.data = objects;
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mRes, parent, false);
String currentUser = getItem(position);
TextView friendName = (TextView) row.findViewById(R.id.tvFriendName);
String Frndname = currentUser;
friendName.setText(Frndname);
TextView notificationView = (TextView) row.findViewById(R.id.tvNotif);
//here i wanted to get the data noOfMsgs
Toast.makeText(mContext, "noOfMsgs:" + numOfMsgs, Toast.LENGTH_LONG).show();
notificationView.setText(noOfMsgs);
return row;
}
}
答案 0 :(得分:1)
您只需要初始化适配器并将适配器附加到ListView
ArrayList<String> items = new ArrayList<>();
items.add(item1);
items.add(item2);
items.add(item3);
CustomArrayAdapter<String> itemsAdapter = new CustomArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
listview.setAdapter(itemsAdapter);
答案 1 :(得分:0)
您只需更新要在CustomArrayAdapter中传递的列表对象,然后通知列表。
adapter.notifyDataSetChanged()
当您将列表对象传递给适配器时,列表中的所有更改都将在对象“数据”中自动更新。您只需要更新视图即可。