CustomArrayAdapter Deployment: Unable to Get Resource ID

I want to implement a CustomArrayAdapter, and the next one is the constructor that I wrote for my adapter

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

Second argument in the super call Resource ID for the layout file containing the TextView to use when creating the instances. I do not know what resource identifier is listed here. Can someone explain in detail what resource identifier is being passed here.

My overridden getView method looks like this: -

  @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; } 
+5
source share
4 answers
  I dont know which resource ID is being referred here. 

Since you put 0 in the second parameter of the constructor, it will not refer to any layout

as the documentation says:

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

Now, if you put an existing layout in the constructor, then this layout will be used to create a view of your listViewItems , but you need to override the getView method getView that you can design what text will be placed where in your layout.

If you plan to host an existing layout, use a different constructor on it:

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

But for design, I usually don’t add the resource layout to the constructor, but directly inflates the view in your getView method and returns that view.

+3
source

Try this way, hope it helps you solve your problem.

 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
source

You need a layout file in this constructor. So you need to change your constructor from

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

to

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

resLayout is the identifier of your layout XML file, which will be in your custom adapter class.

And the syntax for the ArrayAdapter is equal

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

For more information you can check this.

+1
source

I'm not quite sure what you are going to use for the ArrayAdapter , but one of the reasons for using the ArrayAdapter is to display the listView layout depending on the array.

Here is an example of what a constructor should look like

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

Here is what might be in your 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> 

Basically, a CustomListAdapter will generate the number of X layouts of listview_item in height in listView , where X is the size of the array .

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1200646/


All Articles