自定义ListView重复一行

时间:2016-09-03 23:42:07

标签: android listview android-arrayadapter

我创建了一个自定义ListView ,其中包含一个 ImageView 和一个 TextView 。我还从ArrayAdapter 创建了自定义适配器,我使用类的数组(MonsterLink [])来获取适配器的所有信息。但ListView仍在重复一行。我试图在这里找到解决方案(我做了,使用setTag,getTag)但它没有帮助,我真的不明白它。这是我的第一个应用程序。

我调用适配器的CreateView:     @覆盖     public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){         //加载视图         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);
}

我的适配器:

 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;

}

}

我现在看不到任何行

2 个答案:

答案 0 :(得分:1)

尝试从类中扩展适配器

  

BaseAdapter

此外,尝试制作一个本地MonsterLink数组列表以包含适配器中的列表 猜测你不会显示大量的数据,如果你不回收你的观点就好了,因为如果那就是你想要的,我强烈推荐你使用RecyclerView。

尝试以下(未经测试):

    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();
       }

答案 1 :(得分:0)

感谢@Firecat我(我们)发现错误不在适配器中,但是在创建项目列表时。然后将该列表发送到适配器。

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

这将正常工作,但我的对象MonsterLink包含这个:

public class MonsterLink {

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

我不应该使用静态变量! 每次我创建新的MonsterLink时,我都会为数组中的每个对象更改这三个变量。