2011-09-11 42 views
0

此刻我运行了这段代码。我在日食工作,目前正在此错误Android Extendable ListView Child Click

方法的getItem(INT)是未定义Expandable.MySimpleCursorTreeAdapter

 public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, 
      int childPosition, long id) { 
     // use groupPosition and childPosition to locate the current item in the adapter 

      Intent intent = new Intent(Categories.this, com.random.max.Random.class); 
      Cursor cursor = (Cursor) mscta.getItem(childPosition); 
      intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id"))); 
      //Cursor cursor = (Cursor) adapter.getItem(position); 
      //intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id"))); 
      startActivity(intent); 

     return true; 
    } 

回答

0

您使用游标适配器类型,游标可以只能迭代(在一个序列中)。

所以没有getItem(position)方法,因为你不能选择一个特定的项目。 为您的底层DataModel使用不同的适配器,如ArrayAdapter。

这里好了AdapterImplementation

第一部分代码:

YourCustomAdapter extends ArrayAdapter<YourDataObject> 

比你简单实现继承的方法,最重要的方法是getViewgetItem

在getView

定义数据如何将显示在你的ListView中。

使用ViewHolder缓存您的项目进行滚动。

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     View v = convertView; 
     if (v == null) { 
      v = inflater.inflate(layoutResource, null); 
      holder = new ViewHolder(); 
      holder.firstLine = (TextView) v.findViewById(R.id.textview); 
      v.setTag(holder); 
     } else { 
      // Get the ViewHolder back to get fast access to the TextView 
      // and the ImageView. 
      holder = (ViewHolder) v.getTag(); 
     } 
       holder.firstLine = "test"; 
       return v; 
} 

basicly你填写你持有人你的东西,并保存在您的视图中,下一次你不要有重新填写你的资源。

第二种方法getItem(int position)很简单: 您必须指定如何在DataStructure上获取位置“position”上的项目。 如果你有一个阵列,你可以写:

@Override 
public long getItem(int position) { 

    return myDataArray.get(position); 
} 
+0

啊谢谢我不知道这一点。你能提供一个例子或样本吗?谢谢 – Somk