Android 带有传递布尔值的 ArrayAdapter 的自定义 ListView

Android custom ListView with ArrayAdapter passing boolean

我是 Android 的新手,我在将我的布尔标志发送到扩展 ArrayAdapter 的自定义 ListView class 时遇到问题... 我的代码:

public class FriendListView extends ArrayAdapter {
    private final Activity context;
    private final ArrayList<String> photo_url;
    private final ArrayList<String> friend_name;

    private final ArrayList<String> id;
    private final Boolean online;

    public FriendListView(Activity context, ArrayList<String> photo_url, ArrayList<String> friend_name, ArrayList<String> id, Boolean online) {
        super(context, R.layout.friend_view, friend_name);

        this.context = context;
        this.photo_url = photo_url;
        this.friend_name = friend_name;
        this.id = id;
        this.online = online;

        Log.d("VK_LIST_VIEW", "online => " + online); //here is normall pass
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.friend_view, null, true);

        Log.d("VK_POSITION_INDEX", photo_url.get(position));

        TextView txt_friend = (TextView) rowView.findViewById(R.id.txt_friend);

        Log.d("VK_LIST_VIEW_2", "online => " + online); //here I can't acces passed value

        //on friend selecting
        txt_friend.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View view) {
                Log.d("VK_", "friend selected => " + friend_name.get(position) + " id => " + id.get(position) + " is online => " + online);
            }
        });

        txt_friend.setText(friend_name.get(position));
        //show image from url into ImageView
        Picasso.with(context).load(photo_url.get(position)).transform(new CircleTransform()).into((ImageView) rowView.findViewById(R.id.img_photo));
        return rowView;
    }
}

我正在使用第二个代码从另一个 class 传递 online 标志:

adapter = new FriendListView(Logged.this, photo_url, friendArray, friend_id, online);

所以我如何在将它传递给 arrayadapter 时获得在线标志(即在线 = true,然后我在适配器中获得 true)?现在不是这样了....

谁能帮忙解决一下?

您还可以创建一个 List 并保存 boolean 值:

private List<Boolean> onlineList = new ArrayList<>();

然后在constructor中将值保存为:

this.onlineList.add(online);

以后在 getView 中您可以将其用作::

boolean isOnline = this.onlineList.get(position);

为我的英语道歉。

setAdapter()怎么样?

我假设你创建了一个 ListView 可以显示朋友 detail(photo,name,id,weather he is online)

但是我不明白你为什么要用Boolean online?应该是List<Boolean>?因为不是所有的朋友都在线,或者不在线。

我觉得用JavaBean比较好,很简单,你可以google。 如果你用javaBean,就清楚了