2015-04-28 102 views
0

UPDATE添加多種類型objecst的列表視圖與自定義佈局

有什麼辦法很多類型的對象添加到列表視圖與它的適配器? 我的適配器可以處理只是一種類型的對象,保持玩家屬性。但我無法將我的適配器更改爲添加其他類型的對象。我想將GameObjects添加到顯示遊戲屬性的列表中。

public class MyAdapter extends ArrayAdapter<StructureCase> 

當你看到陣列adabter只可以處理,我在上面定義一個對象StructureCase。有了這個,我可以得到物品屬性:

final StructureCase item = getItem(position); 
viewHolder.r_name = (TextView) convertView.findViewById(R.id.textView); 
viewHolder.r_name.setText(item.r_name); 

,這是stucture情況:

public class StructureCase { 

    public int r_ID; 
    public String r_name; 

    public StructureCase() {} 

} 

所以,如果我想有另一種類型的項目喜歡GameStructure,保持遊戲性能,如何解決?

這是我的適配器,我也定義了類型,所以我可以添加列表分隔符每4個項目與它自己的佈局文件,但我應該說,不幸的是,我也使用Structorecase對象添加分隔符,但從來沒有使用他們的內部數據導致我arrayadapter類型是Structurecase :(

public class MyAdapter extends ArrayAdapter<StructureCase> { 

    private LayoutInflater mInflater   = null; 
    public Context context; 
    public Class distinationActivity = null; 
    MediaPlayer myPlayer = new MediaPlayer(); 

    private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>(); 
    private static final int TYPE_ITEM = 0; 
    private static final int TYPE_SEPARATOR = 1; 
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1; 


    public MyAdapter(Context context, int textViewResourceId, List<StructureCase> objects) { 
     super(context, textViewResourceId, objects); 
     mInflater = LayoutInflater.from(context); 

    } 

    public static class ViewHolder { 

     public TextView r_name = null; 
     public TextView r_duration = null; 
     public Button r_send_btn = null; 
     public Button r_play_btn = null; 
     public Button r_del_btn = null; 

     public TextView textView; 

    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder viewHolder ; 
     final View v; 

     final StructureCase item = getItem(position); 
     int type = getItemViewType(position); 

     if (convertView == null) { 

      viewHolder = new ViewHolder(); 

      switch (type) { 
        case TYPE_ITEM: 

         convertView = this.mInflater.inflate(R.layout.record_list_item, null); 
         //mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         convertView = mInflater.inflate(R.layout.record_list_item, parent, false); 
         viewHolder.r_name = (TextView) convertView.findViewById(R.id.textView_name_record_list_item); 
         viewHolder.r_duration = (TextView) convertView.findViewById(R.id.textView_duration_record_list_item); 
         viewHolder.r_send_btn = (Button) convertView.findViewById(R.id.button_send_record_list_item); 
         viewHolder.r_del_btn = (Button) convertView.findViewById(R.id.button_del_record_list_item); 
         viewHolder.r_play_btn = (Button) convertView.findViewById(R.id.button_play_record_list_item); 


         break; 

        case TYPE_SEPARATOR: 


         convertView = mInflater.inflate(R.layout.separator, null); 


         break; 
       } 


      convertView.setTag(viewHolder); 


     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 

     } 


     if(type == 0){ 

      viewHolder.r_name.setText(item.r_name); 
      viewHolder.r_duration.setText(Integer.toString(item.r_duration)); 

      viewHolder.r_play_btn.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 



       } 
      }); 



     }else { 

      viewHolder.textView = (TextView)convertView.findViewById(R.id.textSeparator); 
      viewHolder.textView.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        Toast.makeText(G.currentActivity,"seprator "+position,Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 


     return convertView; 
    } 


    public void addSeparatorItem(final String item) { 
     StructureCase s = new StructureCase(); 

     s.r_name = item ; 
     G.list.add(s); 
     // save separator position 
     mSeparatorsSet.add(G.list.size()-1); 
     Log.i("MAHDI", "G.list.size() = " + G.list.size()); 

     notifyDataSetChanged(); 
    } 



    @Override 
    public int getItemViewType(int position) { 
     return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

,這是項添加到列表的主要活動的一部分:

G.list.clear(); 
    recordingListView.setAdapter(null); 
    mAdapter.clear(); 
    mAdapter.notifyDataSetChanged(); 
    recordingListView.setAdapter(G.mAdapter); 


    if(myCursor.getCount() != 0) for (int i = 1; i <= myCursor.getCount(); i++) { 

     StructureCase sample = new StructureCase(); 
     sample.r_ID = myCursor.getInt(myCursor.getColumnIndex(G.db._ID)); 
     sample.r_name = myCursor.getString(myCursor.getColumnIndex(G.db._NAME)); 

     list.add(sample); 



     if (i % 4 == 0) { 
      mAdapter.addSeparatorItem("separator " + i); 
     } 


    } 
+0

你詢問將不同的佈局添加到列表中? – Paritosh

+0

是的,但具有不同的對象。所以我可以處理他們的數據和屬性來顯示。 – Kenji

+0

你擔心通過不同類型的正確的數據? – Paritosh

回答

0
@Override 
public int getItemViewType(int position) { 
    return type 
} 

@Override 
public int getViewTypeCount() { 
    return count; 
} 

覆蓋這些兩種方法得到不同勢viewtypes ..

而且裏面getview(),只檢查其類型,它並調用UR佈局..在這裏看到的例子。

if (convertView == null) { 
    holder = new ViewHolder(); 
    if (getItemViewType(position) == 0) { 
     convertView = inflater.inflate(R.layout.your_layout_one, parent, false); 
    } else { 
     convertView = inflater.inflate(R.layout.your_layout_two, parent, false); 
    } 
} 
+0

我顯示我的適配器的只是重要組成部分。是的,我用它們,我可以顯示不同類型的意見。但問題是傳遞不同類型的數據。 – Kenji

0

一種解決方案是創建一個類爲:

class SomeData{ 
    String dataType; 
    dataTypeSeperator seperatorData; 
    dataTypeItem ItemData; 
    geters(); 
    seters(); 
} 

現在,在使用此類ArrayList<SomeData>HashMap<key, SomeData>

您可以在objectName.dataType添加條件,如:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view; 
    if(objectName.dataType == "yourDataType") { //condition 
     view = inflater.inflate(R.layout.list_row_text_msg, null); 
    } else if(...){ 
     view = inflater.inflate(R.layout.list_row_image_msg, null); 
    } else { 
     ... 
    } 

    ... 
} 
相關問題