0

I want to display the arrayList items in ListView which is having 2 different textViews. I am using ListViewCustomAdapter and getView(),getItem()... methods are there.

This is my code: MyCustom.java:

public class MyCustom extends BaseAdapter {
  public Activity context;  
  public LayoutInflater inflater; 
  ArrayList mylist;

  public MyCustom(Activity context,ArrayList viewList) {
        super();
        this.context=context;
        this.mylist=viewList;

        inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return mylist.size();
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }


  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View List;
    if(convertView==null)
    {
        List=new View(context);
        LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        List=mLayoutinflater.inflate(R.layout.listitem_row, parent, false);
    }
    else
    {
        List=(View)convertView;

    }

    TextView t1=(TextView)List.findViewById(R.id.txtViewTitle);
    t1.setText((CharSequence) mylist.get(position));

    TextView t2=(TextView)List.findViewById(R.id.txtViewDescription);
    t2.setText(mylist.get(position).toString());


    return List;
  }
}

Above code, there is problem as mentioned below. This will be display the arrayList items as same in both the textviews in ListView. Ex: mylist=['a1','a2','b1','b2'] this is my arraylist and passing to MyCustomAdapter.

At runtime, 'a1' will be displayed in both the textviews.and so on.

I want to display 'a1' in textView1 and 'a2' is in textView2.. and b1 is .. so on...

I think i may have problem in getItem(), getItemId(), getcount() method.. PLEASE HELP ME...

cereallarceny
  • 4,913
  • 4
  • 39
  • 74
EESHA
  • 39
  • 2
  • 6

3 Answers3

2

Its clear from your question that you want to display [a1, a2, b1, b2, c1, c2...] as

a1a2
b1b2
c1c2

so you need to change your code to following:

@Override
public int getCount() {
    // TODO Auto-generated method stub
    if(myList.size()%2==0)
        return mylist.size()/2;
    else
        return myList.size()/2+1;
}

and getView method as below:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View List;
    if(convertView==null)
    {
        List=new View(context);
        LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        List=mLayoutinflater.inflate(R.layout.listitem_row, parent, false);
    }
    else
    {
        List=(View)convertView;

    }

    TextView t1=(TextView)List.findViewById(R.id.txtViewTitle);
    t1.setText((CharSequence) mylist.get(position*2));


    TextView t2=(TextView)List.findViewById(R.id.txtViewDescription);
    if(position*2<getCount())
           t2.setText(mylist.get(position*2+1).toString());


    return List;
}
jeet
  • 29,001
  • 6
  • 52
  • 53
  • is nt this method is doing the same – jeet Mar 09 '12 at 10:07
  • Can u suggest me for Thumbnail URI usages.. I want to display thumbnails in Listview.. These thumbnails are accessed from server.. Ex: http://127.0.0.1:80/playback/2012/a1.jpg. like this so many path will be stored in that above arraylist.. So i should use one path from arraylist and communicate with server and access thumbnail images.. these are displayed in listview.. So arraylist having all thumbnail paths.. Morever, i am using the above arraylist.. – EESHA Mar 09 '12 at 11:50
  • you should look into lazy loading to display images from web. – jeet Mar 09 '12 at 12:20
  • Hi.. Now i am able to display listview with text only.. Here is some problem i may notice. My arraylist has 20 values , but my listview is also shown 20, some of the values are not displayed and some more are interchanged... mylist values looks like ['dasd','vid human','jungle','jungle video last view', .......] upto 30 values.. In listview,some values are missing,if i use above code... Help me.. – EESHA Mar 12 '12 at 08:53
  • I would suggest you to crate a new thread for each new question – jeet Mar 12 '12 at 09:25
0

your adapter getview is perfect, but your logic is wrong.. I would prefer making class object with 2 strings, like.

public class MyClass
{
  String one;
  String two;
}

and make your list like

ArrayList<MyClass> mylist = new ArrayList<MyClass>();

and then setText like you want.

TextView t1=(TextView)List.findViewById(R.id.txtViewTitle);
t1.setText(mylist.get(position).one);           //String one= "a1" according to position in mylist
                                                //it will be = "b1" on next position
                                              //no need of casting to CharSequence

TextView t2=(TextView)List.findViewById(R.id.txtViewDescription);
t2.setText(mylist.get(position).two);           //String two= "a2"
Rohit
  • 2,538
  • 6
  • 28
  • 41
0

You have wrong implementation in some of the adapter methods.

getItem() should return the object from your list at the position:

@Override
public Object getItem(int position) {
    return myList.get(position);
}

Then, in getView

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //  Create view
    //   ...

    String[] item = (String[])getItem(position);  //  Get the current object at 'position'


    //  Update your view here
    TextView t1=(TextView)List.findViewById(R.id.txtViewTitle);
    t1.setText(item[0]);

    TextView t2=(TextView)List.findViewById(R.id.txtViewDescription);
    t2.setText(item[1]);
}

If your want to display two different strings, I suggest you list should look like this

[new String[]{"a1", "a2"}, new String[]{"b1", "b2"}, ...]
Hanon
  • 3,917
  • 2
  • 25
  • 29
  • not working... I want to display 'a1' to textview1 and below that 'a2' to textview2... – EESHA Mar 09 '12 at 09:47
  • Updated. So consider the object structure in the list. For better understanding, each object in a list represent one row. So each object at the list should contains all data about that row. – Hanon Mar 09 '12 at 09:57