gpt4 book ai didi

android - 如何动态创建listView?

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:02 26 4
gpt4 key购买 nike

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

 // 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 而不是布局。

这里是我在 MainActivity() 中的 onCreate 代码

  @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"
/>

最佳答案

您必须将 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;
}

关于android - 如何动态创建listView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com