我創建了一個自定義的的ListView一個ImageView的和一個的TextView。我還創建了定製適配器從ArrayAdapter和我使用類(MonsterLink [])陣列獲取所有信息到適配器。但是ListView仍然重複一行。我試圖在這裏找到解決方案(並且我使用setTag,getTag),但它沒有幫助,我也沒有真正理解它。這是我的第一個應用程序。自定義的ListView被重複一行
CreateView的其中I調用適配器: @Override 公共視圖onCreateView(LayoutInflater充氣,ViewGroup中的容器中,捆綁savedInstanceState){ //加載視圖 this.MyView = inflater.inflate(R.layout.fragment_list,容器,假);
//Change the category label
SetCategoryLabel();
//Load DB;
LoadDatabase();
//Create list for MonsterList
MonsterLink[] MonsterLinkList = LoadMonsterLinkList();
MonsterAdapter monsteradapter = new MonsterAdapter(this.getContext(), MonsterLinkList);
ListView MonsterList = (ListView) MyView.findViewById(R.id.list_Monsters);
MonsterList.setAdapter(monsteradapter);
// Inflate the layout for this fragment
return this.MyView; //inflater.inflate(R.layout.fragment_list, container, false);
}
我的適配器:
public class MonsterAdapter extends ArrayAdapter<MonsterLink> {
public MonsterAdapter(Context context, MonsterLink[] MonsterNames) {
super(context, R.layout.monster_row, MonsterNames);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder mHolder;
// Get the data item for this position
MonsterLink monster = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.monster_row,parent,false);
mHolder = new ViewHolder();
/**TextView MonsterName = (TextView) convertView.findViewById(R.id.txt_MonsterName);
ImageView MonsterPic = (ImageView) convertView.findViewById(R.id.img_MonsterPic);**/
mHolder.mName = (TextView) convertView.findViewById(R.id.txt_MonsterName);
mHolder.mPic = (ImageView) convertView.findViewById(R.id.img_MonsterPic);
convertView.setTag(mHolder);
}else {
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.mName.setText(monster.getName());
return convertView;
}
private class ViewHolder{
private TextView mName;
private ImageView mPic;
}
}
正如我所說的,這是我在Android應用一號和我真的不明白ViewHolder在適配器是如何工作的。
編輯 - 新的適配器 我的適配器現在看起來是這樣的:
public class MonsterAdapter extends BaseAdapter {
@Override
public int getCount() {
return monsterList.size();
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
ArrayList<MonsterLink> monsterList;
Context context;
public MonsterAdapter(Context context, ArrayList<MonsterLink> MonsterNames) {
this.monsterList = MonsterNames;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
MonsterLink monster = monsterList.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.monster_row,parent,false);
TextView name = (TextView) convertView.findViewById(R.id.txt_MonsterName);
ImageView pic = (ImageView) convertView.findViewById(R.id.img_MonsterPic);
name.setText(monster.getName());
return convertView;
}
}
我不能看到任何行現在
你是什麼意思?你可以看到我將MonsterLink [] MonsterNames作爲參數發送給適配器。該數組已被填充。MonsterLink []內部我有兩個對象(兩個名稱),但ListView在每一行上顯示相同的名稱。 – Somachr
有用嗎? @Somachr – Firecat
不,我不能通過超級(上下文);'它說我不能在這裏使用上下文。 – Somachr