I have created a custom ListView with one ImageView and one TextView. I also created custom adapter from ArrayAdapter and I´m using array of classes (MonsterLink[]) to get all informations to the adapter. But the ListView is still repeating one row. I tried to find the solution here (and I did, with the setTag, getTag) but it didn´t help and I dont really understand it. This is my first app.
CreateView where I call the adapter: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Load view this.MyView = inflater.inflate(R.layout.fragment_list, container,false);
//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);
}
My adapter:
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;
}
}
As I said this is my 1st app on Android and I dont really understand how the ViewHolder in the adapter works.
EDIT - New adapter My adapter now looks like this:
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;
}
}
I can´t see any rows now