【问题标题】:Android custom ListView with ArrayAdapter passing boolean带有 ArrayAdapter 的 Android 自定义 ListView 传递布尔值
【发布时间】:2016-08-31 12:11:33
【问题描述】:

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

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

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

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

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

谁能帮忙解决?

【问题讨论】:

  • 描述你的problem?
  • 当您说“我无法访问传递的值”时,您是什么意思?编译器会抛出错误吗?还是值变了?
  • @Abbas 不,编译器不会抛出任何错误,Log.d("VK_LIST_VIEW_2", "online =&gt; " + online); 中的值始终为 false
  • @vladimir 添加完整的类代码。
  • 确保你的在线没有在你的适配器类中声明为 final

标签: java android listview android-arrayadapter android-custom-view


【解决方案1】:

为我的英语道歉。

你是怎么setAdapter()的?

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

但我不明白你为什么使用Boolean online?应该是List&lt;Boolean&gt;吗?因为不是所有的朋友都在线,或者不在线。

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

【讨论】:

    【解决方案2】:

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

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

    然后在constructor中将值保存为:

    this.onlineList.add(online);
    

    以后在getView你可以把它当作::

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多