从 Activity 获取值到自定义数组适配器

Get value from Activity to custom array adapter

我正在创建聊天应用程序。所以我想添加用户从他的朋友那里得到的消息数量。为了表明我创建了自定义 Array adapter,因为我的列表视图由朋友姓名和 notification textview.

组成

所以,我的 list_of_registerd_users activity:

中有数据

如何将此数据发送到自定义 array adapter class 以设置通知视图:

自定义数组适配器class:

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

您只需初始化适配器并将适配器附加到 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);

您只需要更新您在 CustomArrayAdapter 中传递的列表对象,然后通知列表即可。

adapter.notifyDataSetChanged()

当您将列表对象传递给适配器时,列表中的任何更改都会自动更新到对象 'data' 中。您只需更新视图即可。