gpt4 book ai didi

android - 具有 20 种不同类型的行的 ListView( Activity 提要),每行有 20 个不同的 View

转载 作者:行者123 更新时间:2023-11-29 01:47:02 25 4
gpt4 key购买 nike

一周前,我在 stackoverflow 上问了一个关于在其中包含不同种类项目的 ListView 的问题。

I know how to populate a list with custom items via custom adapter. But how do I populate it with a few different types of items?

当时我不知道这会有什么不同,所以我提到我只有 4 种类型的列表项,以免使事情复杂化。我得到了一些回应,其中解释了极其简单的案例(2 种类型的项目,每个项目内部都有一个 View )。我不认为我可以实现我得到的示例中解释的那些方法,我需要一种更面向对象的方法。

我想要一个单独的类,它采用 XML 布局并处理其 View 和按钮。

像这样:

public class MyProfileCommentAdapter extends BaseAdapter {

private ArrayList<String> itemNames = new ArrayList<String>();
private ArrayList<Double> itemRatings = new ArrayList<Double>();
private ArrayList<String> itemComments = new ArrayList<String>();
private ArrayList<String> itemCommentDates = new ArrayList<String>();
private ArrayList<Integer> itemIds = new ArrayList<Integer>();
private Context context;
private LayoutInflater layoutInflater;

private ImageButton iMyCommentsCommentedProductImage;
private TextView tvMyCommentsCommentedItemName;
private TextView tvMyCommentsCommentedItemDate;
private TextView tvMyCommentsCommentedItemContent;
private TextView tvMyCommentsCommentedItemRating;
private Button bMyCommentsEditComment;
private Button bMyCommentsDeleteComment;

public MyProfileCommentAdapter(Context context, ArrayList<String> itemNames, ArrayList<Double> itemRatings, ArrayList<String> itemComments, ArrayList<String> itemCommentDates, ArrayList<Integer> itemIds){

this.context = context;
this.itemNames = itemNames;
this.itemComments = itemComments;
this.itemCommentDates = itemCommentDates;
this.itemRatings = itemRatings;
this.itemIds = itemIds;

layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return itemIds.size();
}

@Override
public Object getItem(int arg0) {
return itemIds.get(arg0) ;
}

@Override
public long getItemId(int position) {
return itemIds.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null) {
convertView = layoutInflater.inflate(R.layout.item_adapterable_my_profile_comment, parent, false);
}

iMyCommentsCommentedProductImage = (ImageButton) convertView.findViewById(R.id.iMyCommentsCommentedProductImage);
tvMyCommentsCommentedItemName = (TextView) convertView.findViewById(R.id.tvMyCommentsCommentedItemName);
tvMyCommentsCommentedItemDate = (TextView) convertView.findViewById(R.id.tvMyCommentsCommentedItemDate);
tvMyCommentsCommentedItemContent = (TextView) convertView.findViewById(R.id.tvMyCommentsCommentedItemContent);
tvMyCommentsCommentedItemRating = (TextView) convertView.findViewById(R.id.tvMyCommentsCommentedItemRating);
bMyCommentsEditComment = (Button) convertView.findViewById(R.id.bMyCommentsEditComment);
bMyCommentsDeleteComment = (Button) convertView.findViewById(R.id.bMyCommentsDeleteComment);

tvMyCommentsCommentedItemName.setText(itemNames.get(position));
tvMyCommentsCommentedItemDate.setText(itemCommentDates.get(position));
tvMyCommentsCommentedItemRating.setText(String.valueOf(itemRatings.get(position)));
tvMyCommentsCommentedItemContent.setText(itemComments.get(position));

return convertView;
}
}

我需要大约 20 个这些并将它们全部放在 ListView 中(我的应用程序的 Activity 提要),具体取决于我的应用程序中最近发生的 Activity 。每个不同的 Activity 都有一个单独的 XML,其中定义了与这个 Activity 一样多的 View 元素,所有 Activity 都在做和显示不同的东西。

现在,如果我要使用建议的方法,

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
Log.v(LOG_TAG, "getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.separator, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
default:
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}

看起来很简单,不是吗?但就我而言,我应该在 SWITCH 中有 20 个不同的案例,每个案例中应该有 20-30 行代码。我不想以这种方式实现它。可悲的是,我不知道该怎么做......

最佳答案

请查看以下链接,希望对解决您的问题有所帮助: Magic Happens here

关于android - 具有 20 种不同类型的行的 ListView( Activity 提要),每行有 20 个不同的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20855668/

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