2012-04-04 9 views
0

クラス1では、私は自分のCustomAdapterに送信するハッシュマップを持っています。CustomAdapterでgetviewが呼び出されない

map.put("year", "Apple");  
map.put("make", "Mango");  
map.put("model", "Grape");  
map.put("style", "Orange");  
map.put("series", "Peach"); 

//link to my adapter 
setListAdapter(new MyCustomAdapter(DynamicLists.this, R.layout.row, map)); 

しかし、クラス2に私のMyCustomAdapter getView関数が呼び出さ取得されていない、あなたはなぜ理解するのを助けることができますか?このアダプタによって表されるデータセット内にあるどのように多くのアイテム

おかげ

CODE

//class 1 
public class DynamicLists extends ListActivity { 

    //class 2 
    public class MyCustomAdapter extends BaseAdapter { 
     String my_VALUES; 
     public MyCustomAdapter(Context context, int textViewResourceId, 
           HashMap<String, String> map) { 

      String[][] array = new String[map.size()][2]; 
      int count = 0; 
      String combined=""; 
      for(Map.Entry<String, String> entry : map.entrySet()){ 
       combined=""+entry.getKey()+""+entry.getValue(); 
       count++; 
      } 
      my_VALUES = combined; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row=convertView; 

      if (row==null){ 
       LayoutInflater inflater=getLayoutInflater(); 
       row=inflater.inflate(R.layout.row, null); 
      } 

      TextView label =(TextView)row.findViewById(R.id.blocked); 
      label.setText(my_VALUES); 

      return row;   
     } 

     @Override 
     public int getCount() { 
      return 0; 
     } 


     @Override 
     public Object getItem(int position) {    
      return null; 
     } 


     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 
    } //end of class 2 


    private static final String TAG = "Example"; 


    public static HashMap<String, String> map = new HashMap<String, String>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //tie data to list, call constructor MyCustomAdapter 
    //populate the list. ArrayAdapter takes current class, layout and array 
    map.put("year", "Apple");  
    map.put("make", "Mango");  
    map.put("model", "Grape");  
    map.put("style", "Orange");  
    map.put("series", "Peach"); 
    //link to my adapter 
    setListAdapter(new MyCustomAdapter(DynamicLists.this, R.layout.row, map));   

    } 


    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     //super.onListItemClick(l, v, position, id); 
     String selection = l.getItemAtPosition(position).toString(); 
     Toast.makeText(this, selection, Toast.LENGTH_LONG).show();   
    }   

} //end of class 1 

答えて

4
getCount() 

http://developer.android.com/reference/android/widget/Adapter.html

@Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return size;// more than zero 
    } 

getItem()何か

@Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return my_VALUES;// may be in your case 
    } 
+0

サイズをどこかに宣言する必要がありますか? – user803271

+0

あなたのマップに含まれる要素の数。 –

+0

例を参照http://android.amberfog.com/?p=296 –

1

を返す必要がありますサイズを宣言する必要があります。 GetCount()関数は、サイズを宣言する必要があります。戻り値を1に設定すると、リストに1つの項目が設定されます。 カーソルを渡している場合は、返信するだけです。cursor.getCount();

スー...そしてあなたはリストに正しく挿入されます。

@Override 
public int getCount() { 
    return cursor.getCount(); 
} 

あなたの場合、マップのサイズを返します。

関連する問題