Customarrayadapter Implementation:unable To Get The Resource Id
Solution 1:
I dont know which resource ID is being referred here.
Since you put 0
in the second parameter of the constructor it wont have any reference to any layout
as the documentation is saying:
resource The resource ID for a layout file containing a layout to use when instantiating views.
Now if you put an existing layout to the constructor then that layout will be use to instantiate the view of your listViewItems
but you need to override the getView
method to enable you to design which text goes where in your layout.
If you are planning to put an existing layout then use another constructor on it:
publicArrayAdapter(Context context, int resource, T[] objects)
But for design I wont really add any resource layout to the constructor but directly inflate the view in your getView
method and return that view.
Solution 2:
Try this way,hope this will help you to solve your problem.
classCustomUsersAdapterextendsBaseAdapter {
private Context context;
pArrayList<User> users;
publicCustomUsersAdapter(Context context,ArrayList<User> users) {
this.context = context;
this.users = users;
}
publicclassViewHolder {
TextView tvName;
TextView tvHometown;
}
@OverridepublicintgetCount() {
return users.size();
}
@Overridepublic Object getItem(int position) {
return users.get(position);
}
@OverridepubliclonggetItemId(int position) {
return position;
}
public View getView(finalint position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = newViewHolder();
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;
}
}
Solution 3:
You need to us your layout file in that constructor. So you need to change your constructor from
public CustomUsersAdapter(Context context, ArrayList<User> users) {
super(context, 0, users);
}
to
publicCustomUsersAdapter(Context context, int resLayout , ArrayList<User> users) {
super(context, resLayout , users);
}
resLayout
is the ID for your layout xml file which will be in your custom adapter class.
And the Syntax for ArrayAdapter
is
publicArrayAdapter(Context context, int resource, T[] objects)
For more info you can check this
Solution 4:
I'm not quite sure what you're going to use your ArrayAdapter
for, but one reason for using an ArrayAdapter
is to display a listView
of a layout depending on the array.
Here is an example of what the constructor should look like
publicCustomListAdapter() {
super(Activity_Main.class, R.layout.listview_item, array);
}
Here is what could be in your listview_item :
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="name"android:id="@+id/name" /></LinearLayout>
Basically, the CustomListAdapter
will generate X number of listview_item
layouts height wise in a ListView
, where X is the size of array
.
Hope this helps.
Post a Comment for "Customarrayadapter Implementation:unable To Get The Resource Id"