更新
有没有办法用它的适配器将多种类型的对象添加到列表视图中? 我的适配器只能处理一种保持玩家属性的对象。但我无法将我的适配器更改为添加其他类型的对象。我想将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对象来添加分隔符,但从未使用过它们的内部数据导致我的arrayadapter类型是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);
}
}
答案 0 :(得分:0)
@Override
public int getItemViewType(int position) {
return type
}
@Override
public int getViewTypeCount() {
return count;
}
覆盖这两种方法以获得不同的视图类型..
在getview()中,只需检查它是哪种类型并调用你的布局..请参阅此处的示例。
if (convertView == null) {
holder = new ViewHolder();
if (getItemViewType(position) == 0) {
convertView = inflater.inflate(R.layout.your_layout_one, parent, false);
} else {
convertView = inflater.inflate(R.layout.your_layout_two, parent, false);
}
}
答案 1 :(得分:0)
一种解决方案是创建一个类:
class SomeData{
String dataType;
dataTypeSeperator seperatorData;
dataTypeItem ItemData;
geters();
seters();
}
现在在ArrayList<SomeData>
或HashMap<key, SomeData>
您可以在objectName.dataType
添加条件,例如:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(objectName.dataType == "yourDataType") { //condition
view = inflater.inflate(R.layout.list_row_text_msg, null);
} else if(...){
view = inflater.inflate(R.layout.list_row_image_msg, null);
} else {
...
}
...
}