Android Extendable ListView Child Click
At the moment I have this code running. I am working in eclipse and at the moment am getting this error
The method getItem(int) is undefined for the type 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开发者_StackOverflow.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;
}
You using a Cursor Adapter, Cursors can only be iterated over (in a sequence).
So there is no getItem(position)
method because you cant pick a specific item.
Use a different Adapter for your underlying DataModel like a ArrayAdapter.
Ok here some code for a AdapterImplementation
first:
YourCustomAdapter extends ArrayAdapter<YourDataObject>
than you simple implement the inherited Methods, the important Methods are getView
and getItem
in getView you define how the Data will be shown in your ListView.
Use ViewHolder to cache your Items for scrolling.
@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 you fill your holder with your stuff and save it inside your View, the next time your dont have to fill in your Resources again.
the second method getItem(int position) is simple: you have to specify how to get the item on position "position" on your DataStructure. If you have an Array you could write:
@Override
public long getItem(int position) {
return myDataArray.get(position);
}
精彩评论