0

2 つの異なる textView を持つ ListView に arrayList アイテムを表示したいと考えています。ListViewCustomAdapter と getView(),getItem()... メソッドを使用しています。

これは私のコードです: 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;
  }
}

上記のコードには、以下のような問題があります。これにより、ListView の両方のテキストビューで arrayList アイテムが同じように表示されます。例: mylist=['a1','a2','b1','b2'] これは私の配列リストで、MyCustomAdapter に渡します。

実行時に、'a1' が両方のテキストビューに表示されます。

「a1」をtextView1に表示し、「a2」をtextView2に表示したい..そしてb1は..など...

getItem()、getItemId()、getcount() メソッドに問題があると思います。助けてください...

4

3 に答える 3

2

[a1、a2、b1、b2、c1、c2...]を次のように表示することはあなたの質問から明らかです。

a1a2
b1b2
c1c2

したがって、コードを次のように変更する必要があります。

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

およびgetViewメソッドは次のとおりです。

@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;
}
于 2012-03-09T09:18:57.707 に答える
0

アダプターの getview は完璧ですが、ロジックが間違っています。

public class MyClass
{
  String one;
  String two;
}

リストを次のようにします

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

そして、必要に応じてテキストを設定します。

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"
于 2012-03-09T09:15:57.763 に答える
0

一部のアダプタ メソッドで実装が間違っています。

getItem() は、次の位置にあるリストからオブジェクトを返す必要があります。

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

次に、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]);
}

2 つの異なる文字列を表示したい場合は、リストを次のようにすることをお勧めします

[new String[]{"a1", "a2"}, new String[]{"b1", "b2"}, ...]
于 2012-03-09T09:17:08.203 に答える