【问题标题】:CustomArrayAdapter Implementation:Unable to get the resource idCustomArrayAdapter 实现:无法获取资源id
【发布时间】:2014-08-19 04:49:21
【问题描述】:

我想实现 CustomArrayAdapter,下面是我为自定义适配器编写的构造函数

public CustomUsersAdapter(Context context, ArrayList<User> users) {
        super(context, 0, users);
     }

super 调用中的第二个参数 包含 TextView 的布局文件的资源 ID,以在实例化视图时使用。我不知道这里引用的是哪个资源 ID。谁能详细解释一下这里引用的是哪个资源ID。

我重写的 getView 方法如下:-

 @Override
     public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        User user = getItem(position);    
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
           convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
        }
        // Lookup view for data population
        TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
        TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
        // Populate the data into the template view using the data object
        tvName.setText(user.name);
        tvHome.setText(user.hometown);
        // Return the completed view to render on screen
        return convertView;
    }

【问题讨论】:

  • 那是你的layout id,你可以通过构造函数传递或者直接插入那里。
  • 第二个参数是您的 ListView 行布局 ID,就像您在 getView() 中使用的一样。
  • 我已经编辑了我的问题以包含 getView 方法。 .你能告诉我这是正确的方法吗?

标签: android custom-adapter


【解决方案1】:
 I dont know which resource ID is being referred here.

由于您将0 放在构造函数的第二个参数中,因此它不会引用任何布局

正如documentation 所说:

 resource   The resource ID for a layout file containing a layout to use when instantiating views.

现在,如果您将现有布局放入构造函数,那么该布局将用于实例化 listViewItems 的视图,但您需要覆盖 getView 方法以使您能够设计哪些文本在布局中的位置.

如果您打算放置现有布局,请在其上使用另一个构造函数:

public ArrayAdapter (Context context, int resource, T[] objects)

但对于设计,我不会真正向构造函数添加任何资源布局,而是直接膨胀您的 getView 方法中的视图并返回该视图。

【讨论】:

  • 我已经编辑了我的问题以包含 getView 方法。 .你能告诉我这是正确的方法吗?
  • @AnkitGarg 正确的方法是 viewHolder 模式。速度很快。
  • @AnkitGarg 转到此链接以了解如何实现 View holder 模式codeofaninja.com/2013/09/…
  • 如果有的话,能否在 github 上给我一些示例代码?
  • @AnkitGarg 只需遵循如何实现 voew holder 模式的模式,它将提高您的 listView 的性能
【解决方案2】:

试试这个方法,希望能帮助你解决问题。

class CustomUsersAdapter extends BaseAdapter {

    private Context context;
    pArrayList<User> users;


    public CustomUsersAdapter(Context context,ArrayList<User> users) {
        this.context = context;
        this.users = users;
    }

    public class ViewHolder {
        TextView tvName;
        TextView tvHometown;
    }


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

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

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

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_user, null);
            holder.tvName = (TextView) view.findViewById(R.id.tvHometown);
            holder.tvHometown = (TextView) view.findViewById(R.id.tvHometown);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tvName.setText(users.get(position).name);
        holder.tvHometown.setText(users.get(position).hometown);
        return view;
    }
}

【讨论】:

  • 我想这是由viewholder实现的baseadapter,这比我之前采用的方法要快。我说的对吗?
【解决方案3】:

您需要在该构造函数中使用您的布局文件。所以你需要从

改变你的构造函数
public CustomUsersAdapter(Context context, ArrayList<User> users) {
    super(context, 0, users);
 }

public CustomUsersAdapter(Context context, int resLayout , ArrayList<User> users) {
    super(context, resLayout , users);
 }

resLayout 是您的布局 xml 文件的 ID,它将在您的自定义适配器类中。

ArrayAdapter 的语法是

public ArrayAdapter (Context context, int resource, T[] objects)

更多信息可以查看this

【讨论】:

  • 我已经编辑了我的问题以包含 getView 方法。 .你能告诉我这是正确的方法吗?
  • 您只需要更改您的适配器构造函数,您已将适配器设置为列表视图我在回答中所做的更改。
【解决方案4】:

我不太确定您要将ArrayAdapter 用于什么用途,但使用ArrayAdapter 的一个原因是根据阵列显示布局的listView

这是构造函数应该是什么样子的示例

public CustomListAdapter() {
    super(Activity_Main.class, R.layout.listview_item, array);
}

这是您的 listview_item 中可能包含的内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="name"
        android:id="@+id/name" />

</LinearLayout>

基本上,CustomListAdapter 将在 ListView 中生成 X 个高度的 listview_item 布局,其中 X 是 array 的大小。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多