overriding custom adapter for list view

80 Views Asked by At

I am new to android development.
I am trying to create an arrayAdapter for my listView. I want my listView to contain one image and two TextViews. For this I have override the constructor of an ArrayAdapter and also overided the getView method.
My app stops everytime I try to run it. I couldn't find the bug in my code.

public class main extends ActionBarActivity {

String[] frnds;
String[] depName;
int[] images = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i};
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    lv = (ListView) findViewById(R.id.listView1);

    Resources res = getResources();

    frnds = res.getStringArray(R.array.names);
    depName = res.getStringArray(R.array.description);

    customAdapter adapter = new customAdapter(this, frnds, depName, images);
    lv.setAdapter(adapter);

}
}


class customAdapter extends ArrayAdapter<String> {

Context context;
String[] name;
String[] description;
int[] imgs;

customAdapter(Context c, String[] names, String[] desc, int img[]) {
    super(c, R.layout.single_row, R.id.textView1, names);
    this.context = c;
    this.imgs = img;
    this.name = names;
    this.description = desc;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.single_row, parent, false);

    TextView tv1 = (TextView) row.findViewById(R.id.textView1);
    TextView tv2 = (TextView) row.findViewById(R.id.textView2);
    ImageView iv = (ImageView) row.findViewById(R.id.imageView1);

    tv1.setText(name[position]);
    tv2.setText(description[position]);
    iv.setImageResource(imgs[position]);

    return row;
}
}
1

There are 1 best solutions below

0
On

The problem I faced was because of memory issue.
I try to use the images from drawable folder which takes lot of space and because of that "Out of memory" error is shown.
Furthermore, the code for overriding getView is also not an effective one.

I am trying to find the solution for this and will try to keep you update.