0
votes

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

2

2 Answers

1
votes

Try extending your adapter from the class

BaseAdapter

And also, try making a local MonsterLink array list to contain the list in the adapter Guessing you're not going to show tons of data, it's ok if you don't recycle your views, because if thats what you want, I highly recommend you RecyclerView.

Try the following (not tested):

    public class MonsterAdapter extends BaseAdapter {

        ArrayList<MonsterLink> monsterList;
        Context context;
        public MonsterAdapter(Context context, 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 MonsterName = (TextView) convertView.findViewById(R.id.txt_MonsterName);
        ImageView MonsterPic = (ImageView) convertView.findViewById(R.id.img_MonsterPic);**/

        TextView name = (TextView) convertView.findViewById(R.id.txt_MonsterName);
        ImageView pic = (ImageView) convertView.findViewById(R.id.img_MonsterPic);
        //name.setText('bla bla')
        //name.setText(monster.getName())
        return convertView;

       }


       @Override
       public int getCount() {
          return monsterList.size();
       }
0
votes

Thanks to @Firecat I (we) have located that the error is not in the adapter but when the list of items is created. The list is then sent to the Adapter.

while (cursor.moveToNext()){
            List.add(new MonsterLink(cursor.getString(0),cursor.getString(1),cursor.getString(2)));
        }
        cursor.close();

This will normaly work fine but my object MonsterLink contained this:

public class MonsterLink {

private Static String Name;
private Static String PicPath;
private Static String MonsterID;

I shouldn´t have use static variables! Everytime I have created new MonsterLink I changed these three variables for every object in the array.