简体   繁体   English

自定义ListView重复一行

[英]Custom ListView is repeating one row

I have created a custom ListView with one ImageView and one TextView . 我创建了一个具有一个ImageView和一个TextView自定义ListView I also created custom adapter from ArrayAdapter and I´m using array of classes (MonsterLink[]) to get all informations to the adapter. 我还从ArrayAdapter创建了自定义适配器,并使用类数组(MonsterLink [])将所有信息获取到适配器。 But the ListView is still repeating one row. 但是ListView仍在重复一行。 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. 我试图在这里找到解决方案(我使用setTag,getTag做到了),但是并没有帮助,我也不是很了解。 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); 我在其中调用适配器的CreateView:@Override 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);
}

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. 正如我所说的,这是我在Android上的第一个应用程序,我不太了解适配器中的ViewHolder是如何工作的。

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 我现在看不到任何行

Try extending your adapter from the class 尝试从类中扩展适配器

BaseAdapter 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. 另外,请尝试制作一个本地MonsterLink数组列表以将其包含在适配器中,以确保您不会显示大量数据,如果不回收视图也可以,因为如果那是您想要的,我强烈建议您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();
       }

Thanks to @Firecat I (we) have located that the error is not in the adapter but when the list of items is created. 感谢@Firecat,我(我们)已经找到了错误不在适配器中,而是在创建项目列表时出现的错误。 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: 这通常可以正常工作,但是我的对象MonsterLink包含以下内容:

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. 每次创建新的MonsterLink时,我都会为数组中的每个对象更改这三个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM