如何使用android中的arrayadapter在代码中创建布局

时间:2012-08-02 11:44:39

标签: android android-arrayadapter

我这里有这个代码:

 CustomAdapter adapter = new CustomAdapter(myActivity.this, R.layout.listitemrow,
                        catalogList);
 myListView.setAdapter(adapter);
 ....


 class CustomAdapter:


 public class CustomAdapter extends ArrayAdapter<String>{

    public CustomAdapter(Activity a, int textViewResourceId, ArrayList<String> tweets){
                ....
    }
    ....
  } 

listitemrow是android布局文件夹中的xml。 出于某种原因,我不想使用R.layout.listitemrow。我想使用在代码中创建的mylayout:

  public class myLayout extends RelativeLayout{
      .....
  }

有没有这样做?像这样:

  CustomAdapter adapter = new CustomAdapter(myActivity.this, new myLayout() ,
                            catalogList);

谢谢。

1 个答案:

答案 0 :(得分:0)

您应该覆盖适配器的getView()方法:

public class CustomAdapter extends ArrayAdapter{

    public CustomAdapter(Activity a, int textViewResourceId, ArrayList<String> tweets){
        ....
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View itemview = new myLayout(parent.getContext());
        ...
        return itemView;
    }

   ....
}