0

在第 1 类中,我有一个哈希图,我将它发送到我的 CustomAdapter。

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 类中,我的 MyCustomAdaptergetView函数没有被调用,你能帮助理解为什么吗?

谢谢

代码

//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

2 回答 2

4
getCount() 

此 Adapter 表示的数据集中有多少项。 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
    }
于 2012-04-04T12:51:57.960 回答
1

必须声明尺寸。您的GetCount()函数必须声明大小... 将返回值设置为 1,您的列表将由一项填充。所以如果你传入一个游标,只需返回cursor.getCount();

Soo... 你的列表将被正确填充。

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

在您的情况下,返回地图的大小。

于 2012-12-17T04:18:20.437 回答