【发布时间】:2015-04-28 13:10:19
【问题描述】:
更新
有没有办法通过它的适配器将多种类型的对象添加到列表视图中? 我的适配器只能处理一种保留玩家属性的对象。但我无法将适配器更改为添加另一种类型的对象。我想将 GameObjects 添加到显示游戏属性的列表中。
public class MyAdapter extends ArrayAdapter<StructureCase>
如您所见,Array adabter 只能处理我在上面定义为 StructureCase 的一个对象。有了这个我可以得到项目属性:
final StructureCase item = getItem(position);
viewHolder.r_name = (TextView) convertView.findViewById(R.id.textView);
viewHolder.r_name.setText(item.r_name);
这是结构案例:
public class StructureCase {
public int r_ID;
public String r_name;
public StructureCase() {}
}
所以如果我想拥有另一种类型的项目,例如保留游戏属性的 GameStructure,那么解决方案是什么?
这是我的适配器,我还定义了类型,因此我可以使用它自己的布局文件每 4 个项目添加列表分隔符,但我应该说不幸的是我也使用了 Structorecase 对象来添加分隔符,但从未使用它们的内部数据导致我的数组适配器类型是 Structurecase :(
public class MyAdapter extends ArrayAdapter<StructureCase> {
private LayoutInflater mInflater = null;
public Context context;
public Class distinationActivity = null;
MediaPlayer myPlayer = new MediaPlayer();
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
public MyAdapter(Context context, int textViewResourceId, List<StructureCase> objects) {
super(context, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
}
public static class ViewHolder {
public TextView r_name = null;
public TextView r_duration = null;
public Button r_send_btn = null;
public Button r_play_btn = null;
public Button r_del_btn = null;
public TextView textView;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder ;
final View v;
final StructureCase item = getItem(position);
int type = getItemViewType(position);
if (convertView == null) {
viewHolder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = this.mInflater.inflate(R.layout.record_list_item, null);
//mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.record_list_item, parent, false);
viewHolder.r_name = (TextView) convertView.findViewById(R.id.textView_name_record_list_item);
viewHolder.r_duration = (TextView) convertView.findViewById(R.id.textView_duration_record_list_item);
viewHolder.r_send_btn = (Button) convertView.findViewById(R.id.button_send_record_list_item);
viewHolder.r_del_btn = (Button) convertView.findViewById(R.id.button_del_record_list_item);
viewHolder.r_play_btn = (Button) convertView.findViewById(R.id.button_play_record_list_item);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.separator, null);
break;
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if(type == 0){
viewHolder.r_name.setText(item.r_name);
viewHolder.r_duration.setText(Integer.toString(item.r_duration));
viewHolder.r_play_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}else {
viewHolder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
viewHolder.textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(G.currentActivity,"seprator "+position,Toast.LENGTH_SHORT).show();
}
});
}
return convertView;
}
public void addSeparatorItem(final String item) {
StructureCase s = new StructureCase();
s.r_name = item ;
G.list.add(s);
// save separator position
mSeparatorsSet.add(G.list.size()-1);
Log.i("MAHDI", "G.list.size() = " + G.list.size());
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public long getItemId(int position) {
return position;
}
这是将项目添加到列表的主要活动的一部分:
G.list.clear();
recordingListView.setAdapter(null);
mAdapter.clear();
mAdapter.notifyDataSetChanged();
recordingListView.setAdapter(G.mAdapter);
if(myCursor.getCount() != 0) for (int i = 1; i <= myCursor.getCount(); i++) {
StructureCase sample = new StructureCase();
sample.r_ID = myCursor.getInt(myCursor.getColumnIndex(G.db._ID));
sample.r_name = myCursor.getString(myCursor.getColumnIndex(G.db._NAME));
list.add(sample);
if (i % 4 == 0) {
mAdapter.addSeparatorItem("separator " + i);
}
}
【问题讨论】:
-
您是在询问要添加到列表中的不同布局吗?
-
是的,但对象不同。所以我可以处理他们的数据和属性来显示。
-
您是否担心传递不同类型的数据?
-
... 是的。没错。
-
您要交替显示
structureCase和Seperators吗?你能分享一些关于它的细节吗?或者是数据的类型,还是listView要显示的部分?
标签: android android-layout listview object android-arrayadapter