4

I want to implement CustomArrayAdapter and following is the constructor I have written for my custom adapter

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

The second argument in the super call The resource ID for a layout file containing a TextView to use when instantiating views. I dont know which resource ID is being referred here. Can anyone please explain in detail which resource ID is being referred here.

My overridden getView method is as follows :-

 @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;
    }
LearningBasics
  • 660
  • 1
  • 7
  • 24

4 Answers4

3
 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:

public ArrayAdapter (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.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • i have edited my question to include the getView method . . can you please tell is this the right approach? – LearningBasics Aug 19 '14 at 05:12
  • @AnkitGarg the right approach is a viewHolder pattern. which is fast. – Rod_Algonquin Aug 19 '14 at 05:16
  • @AnkitGarg go to this link to learn how to implement View holder pattern http://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html – Rod_Algonquin Aug 19 '14 at 05:17
  • can you provide me some sample code on github for the same if you have ? – LearningBasics Aug 19 '14 at 05:18
  • @AnkitGarg just follow the pattern on how to implement voew holder pattern it will increase the performance of your listView – Rod_Algonquin Aug 19 '14 at 05:20
  • Thanks it worked for me. Do you think you could help me with this question : http://stackoverflow.com/questions/25549679/how-can-i-split-a-long-single-sqliteopenhelper-into-serveral-classes-one-for-e – eddy Aug 28 '14 at 13:59
2

Try this way,hope this will help you to 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;
    }
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • this is implemented of baseadapter by viewholder I suppose, which is faster than the approach adopted by me earlier. I am correct ? – LearningBasics Aug 19 '14 at 05:37
1

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

public CustomUsersAdapter(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

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

For more info you can check this

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • i have edited my question to include the getView method . . can you please tell is this the right approach? – LearningBasics Aug 19 '14 at 05:14
  • you just need to change your adapter constructor where you have set adapter to your listview what i have changed in my answer. – Piyush Aug 19 '14 at 05:15
1

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

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

Here is what could 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, 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.

crejaud
  • 65
  • 3