2016-09-03 73 views
0

我創建了一個自定義的的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; 

} 

}

我不能看到任何行現在

回答

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

你是什麼意思?你可以看到我將MonsterLink [] MonsterNames作爲參數發送給適配器。該數組已被填充。MonsterLink []內部我有兩個對象(兩個名稱),但ListView在每一行上顯示相同的名稱。 – Somachr

+0

有用嗎? @Somachr – Firecat

+0

不,我不能通過超級(上下文);'它說我不能在這裏使用上下文。 – Somachr

0

感謝@Firecat我(我們)已經找到錯誤是不是在適配器,但在創建項目的列表時。該列表然後發送到適配器。

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

這將normaly工作得很好,但我的目標MonsterLink包含在此:

public class MonsterLink { 

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

我shouldn't有使用靜態變量! 每次我創建了新的MonsterLink,我都爲數組中的每個對象更改了這三個變量。