0

私の状況は次のとおりです。私は2つのレイアウトを持っています

初め:

<RelativeLayout ... >
    <TextView id="@+id/textview1" .../>
    <TextView id="@+id/textview2" .../>
    <ImageView .../>
</RelativeLayout>  

2番:

<RelativeLayout ... >
    <TextView id="@+id/textview3" .../>
    <TextView id="@+id/textview4" .../>
    <ImageView .../>
</RelativeLayout>

次に、これら 2 つのレイアウトでリストビューを作成します。ArrayAdapter の準備が整いました。ここで、アダプターの新しいインスタンスを作成する必要がありますが、ここで立ち往生しています。そのコンストラクターは次のとおりです。

public ArrayAdapter (Context context, 
    int resource, 
    int textViewResourceId, 
    List<T> objects)

context     The current context.
resource    The resource ID for a layout file containing a layout to use when instantiating views.
textViewResourceId  The id of the TextView within the layout resource to be populated
objects     The objects to represent in the ListView.

問題 & 質問:
2 番目と 3 番目のパラメーターの処理方法がわかりません。2 つのパラメーターのそれぞれに複数の選択肢があるようです。アダプターを開始するにはどうすればよいですか?

4

1 に答える 1

0

独自のアダプタを作成し、おそらく BaseAdapter から拡張して getView() メソッドを実装し、必要な各アイテムに応じてレイアウトを変更してください。このコードを確認してください。これが役立つ場合があります。

public class MyAdapter extends BaseAdapter{

    private ArrayList<String> data;
    private LayoutInflater inflater;
    public MyAdapter(ArrayList<String> data){
        this.data = data;
        //This is your data from outside

    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        View view = convertView;
        if(view == null){
            if(condition for layout 1){
                // inflate layout 1 to your view here
                            // import data from data.get(position) into you view here
            }else if(condition for layout 2){
                // inflate layout 2 for your view here
                            // import data from data.get(position) into you view here
            }
        }
        return view;
    }

}
于 2013-08-08T04:26:25.727 に答える