如何动态创建listView?

时间:2015-06-22 16:05:07

标签: android android-layout listview

我有一个任务,我必须创建一个ListView,其中每一行应包含图像,标题和详细信息。但我需要在没有任何XML文件的情况下以programmaticaly方式创建它。所以我的问题是我做了什么我做错了?这是我的解决方案:

 // My custom View which represents the row of ListView   
 public class CustomListItem extends LinearLayout{

//region Private fields
private TextView listItemTitle_;
private TextView listItemDetails_;
private ImageView listItemImage_;
//endregion

//region Public constructors
public CustomListItem(Context context) {
    super(context);
    init(null, 0);
}

public CustomListItem(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    init(attributeSet, 0);
}

public CustomListItem(Context context, AttributeSet attributeSet, int defStyle) {
    super(context, attributeSet, defStyle);
    init(attributeSet, defStyle);
}
//endregion

//region Accessors
public void setListItemTitle(String title) {
    this.listItemTitle_.setText(title);
}

public String getTitle() {
    return listItemTitle_.getText().toString();
}

public void setListItemDetails(String details) {
    this.listItemDetails_.setText(details);
}

public String getListItemDetails() {
    return listItemDetails_.getText().toString();
}

//endregion

private void init(AttributeSet attributeSet, int defStyle) {
    LinearLayout linearLayout = new LinearLayout(getContext());
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setPadding(0, 70, 0, 0);
    listItemTitle_ = new TextView(getContext());
    listItemTitle_.setText("Title");

    listItemDetails_  = new TextView(getContext());
    listItemDetails_.setText("Details");

    listItemImage_ = new ImageView(getContext());
    listItemImage_.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.clear));

    addView(linearLayout);
    linearLayout.addView(listItemTitle_);
    linearLayout.addView(listItemDetails_);
}

}

这是我的适配器

   public class ItemAdapter extends ArrayAdapter<ItemInfo> {

public ItemAdapter(Context context, List<ItemInfo> items) {
    super(context, R.layout.custom_list_item, items);

}

@Override
public View getView(int postition,View convertView, ViewGroup parent){
    ItemInfo item = this.getItem(postition);
    CustomListItem customListItem_ = new CustomListItem(getContext());
    customListItem_.setListItemTitle(item.getTitle());
    customListItem_.setListItemTitle(item.getDescription());
    return convertView;
}

}

但我不需要使用R.layout.custom_list_item,我需要使用我的customView而不是布局。

这里我的代码onCreate in MainActivity()

  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    LinearLayout layout = new LinearLayout(getApplicationContext());
    ListView listView = new ListView(getApplicationContext());

    CustomListItem customListItem = new CustomListItem(getApplicationContext());

    List<ItemInfo> values = new ArrayList<ItemInfo>();
    Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.clear);
    ItemInfo info = new ItemInfo(image,"Hi","android");
    ItemInfo info2 = new ItemInfo(image,"Hi2","android");
    values.add(info);
    values.add(info2);

    Adapter_ = new ItemAdapter(this, values);

    setContentView(R.layout.activity_main);

    layout.addView(listView);
    setContentView(layout);
}

这里是main_activity布局

xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<com.example.admin.lab_127.CustomListItem
    android:id="@+id/custom_vie2w"
    android:layout_width = "wrap_content"
    android:layout_height="wrap_content"
    />

2 个答案:

答案 0 :(得分:2)

您必须将Adapter_设置为ListView

listView.setAdapter(Adapter_);

另外,行:

你的getView应该返回值:

@Override
public View getView(int postition,View convertView, ViewGroup parent){
    ItemInfo item = this.getItem(postition);
    CustomListItem customListItem_ = new CustomListItem(getContext());
    customListItem_.setListItemTitle(item.getTitle());
    customListItem_.setListItemTitle(item.getDescription());
    return customListItem;
}

答案 1 :(得分:1)

这是我的适配器

 @Override
   public View getView(int position, View convertView, ViewGroup parent) {
    HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());

    LinearLayout layout = new LinearLayout(getContext());
    layout.setLayoutParams(new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
    ));

    float paddingLeft = getContext().getResources().getDimension(R.dimen.padding_left_for_image);
    layout.setPadding((int) paddingLeft, 0, 0, 0);

    ItemInfo item = this.getItem(position);
    CustomListItem customListItem_ = new CustomListItem(getContext());
    customListItem_.setListItemTitle(item.getTitle());
    customListItem_.setListItemDetails(item.getDescription());
    customListItem_.setListItemImage(item.getImage());

    layout.addView(customListItem_);

    horizontalScrollView.addView(layout);
    convertView = horizontalScrollView;
    return convertView;
}

这里是MainActivity中的onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(getApplicationContext());
    layout.setOrientation(LinearLayout.VERTICAL);
    ListView listView = new ListView(this);

    listView.setPadding(0, (int)getResources().getDimension(R.dimen.list_item_top), 0, 0);

    listView.setDivider(null);

    List<ItemInfo> values = new ArrayList<ItemInfo>();
    Drawable image = getResources().getDrawable(R.mipmap.clear);

    for (int i = 0; i < 20; i++) {
        ItemInfo info = new ItemInfo(image, getResources().getString(R.string.title) + i, getResources().getString(R.string.details) + i);
        values.add(info);
    }

    Adapter_ = new ItemAdapter(this, values);
    listView.setAdapter(Adapter_);
    layout.addView(listView);
    setContentView(layout);
}

希望它有用