【问题标题】:ListView - Custom Adapter without LayoutInflatorListView - 没有 LayoutInflator 的自定义适配器
【发布时间】:2016-04-11 10:58:32
【问题描述】:

到目前为止,我尝试遵循的每个教程都试图让我在 CustomAdapter 的 getView() 中使用 LayoutInflator。

LayoutInflator 没有出现,我只能选择 LayoutInflatorCompat 或 LayoutFactory。

谁能帮我从我创建的用户对象中取出数据并将其设置为我的 rowlayout.xml 中的 2 个适当的文本字段?

我假设这仍然需要在我的 UserAdapter 中的 getView() 方法中完成。

用户只有 2 个字符串,名称和描述。我的 rowlayout 有 tv_name、tv_description。

public class UserAdapter extends BaseAdapter {


ArrayList<user> list;
Context context;

public UserAdapter(Context c,ArrayList<user> list)
{
    context = c;
    this.list = list;

}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    return null;
}

}

【问题讨论】:

  • 你是什么意思它不会出现在你身上?
  • 它不是作为变量的选项。也许我会重置我的电脑,看看它是否出现:/
  • 添加导入android.view.LayoutInflater;在顶部,看看它是否有帮助

标签: android listview baseadapter layout-inflater


【解决方案1】:

使用LayoutInflater 和您的Context 将所需的布局填充到convertView,然后您可以使用它来获取TextViews 并设置文本,如下所示:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.your_layout, parent, false);
            TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
            TextView tvDescription = (TextView) convertView.findViewById(R.id.tv_description);

            User user = users.get(position);
            tvName.setText(user.getName());
            tvDescription.setText(user.getDescription());
        }
        return convertView;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多