CustomArrayAdapter实现:无法获取资源ID

时间:2014-08-19 04:49:22

标签: android custom-adapter

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

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

超级调用中的第二个参数包含在实例化视图时使用的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;
    }

4 个答案:

答案 0 :(得分:3)

 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方法中的视图并返回该视图。

答案 1 :(得分: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;
    }
}

答案 2 :(得分:1)

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

更改构造函数
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

答案 3 :(得分:1)

我不太确定您要使用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_item中以高度方式生成X个ListView布局,其中X是array的大小。

希望这有帮助。