Reputation: 325
I'm trying to make an app that implements a custom Listview
with Headers
. But I get an error ClassCastException
. This is this my log and code:
03-26 10:53:47.385: E/AndroidRuntime(2219): Caused by: java.lang.ClassCastException: com.hb.examples.MainActivity cannot be cast to android.app.ListActivity
03-26 10:53:47.385: E/AndroidRuntime(2219): at com.hb.examples.PinnedSectionListActivity.initializeAdapter(PinnedSectionListActivity.java:139)
03-26 10:53:47.385: E/AndroidRuntime(2219): at com.hb.examples.MainActivity.onCreate(MainActivity.java:17)
It is this my code:
MAIN
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PinnedSectionListActivity.initializeAdapter(this);
}
}
PinnedSectionListActivity
public class PinnedSectionListActivity extends ListActivity implements OnClickListener {
static class SimpleAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter {
private static final int[] COLORS = new int[] {
R.color.green_light, R.color.orange_light,
R.color.blue_light, R.color.red_light };
public SimpleAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
final int sectionsNumber = 5;
final String[] iniciales = {"A", "C", "F", "I", "V"};
final String[][] nombres = {{"A", "B"}, {"H"}, {"D", "G", "I"}, {"D"}, {"F"}};
prepareSections(sectionsNumber);
int sectionPosition = 0, listPosition = 0;
for (char i=0; i<sectionsNumber; i++) {
Item section = new Item(Item.SECTION, iniciales[i]);
section.sectionPosition = sectionPosition;
section.listPosition = listPosition++;
onSectionAdded(section, sectionPosition);
add(section);
final int itemsNumber = nombres[i].length;
for (int j=0;j<itemsNumber;j++) {
Item item = new Item(Item.ITEM, nombres[i][j]);
item.sectionPosition = sectionPosition;
item.listPosition = listPosition++;
add(item);
}
sectionPosition++;
}
}
protected void prepareSections(int sectionsNumber) { }
protected void onSectionAdded(Item section, int sectionPosition) { }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.DKGRAY);
view.setTag("" + position);
Item item = getItem(position);
if (item.type == Item.SECTION) {
view.setBackgroundColor(parent.getResources().getColor(COLORS[item.sectionPosition % COLORS.length]));
}
return view;
}
@Override public int getViewTypeCount() {
return 2;
}
@Override public int getItemViewType(int position) {
return getItem(position).type;
}
@Override
public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}
}
static class Item {
public static final int ITEM = 0;
public static final int SECTION = 1;
public final int type;
public final String text;
public int sectionPosition;
public int listPosition;
public Item(int type, String text) {
this.type = type;
this.text = text;
}
@Override
public String toString() {
return text;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Item item = (Item) getListView().getAdapter().getItem(position);
if (item != null) {
Toast.makeText(this, "CLIC 2", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "CLIC 3", Toast.LENGTH_SHORT).show();
}
}
@SuppressLint("NewApi")
public static void initializeAdapter(Context context) {
((ListActivity) context).setListAdapter(new SimpleAdapter(context, android.R.layout.simple_list_item_1, android.R.id.text1));
}
@Override
public void onClick(View v) {
}
}
EDIT:
I want to call from MainActivity
Upvotes: 0
Views: 133
Reputation: 1536
simple activity does not have ListView attached in it, you need to extend your MainActivity
with ListActivity
instead of Activity
.
public class MainActivity extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PinnedSectionListActivity.initializeAdapter(this);
}
}
define your list in layout like
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
list must be define with id android:id="@android:id/list"
Upvotes: 0
Reputation: 157437
@SuppressLint("NewApi")
public static void initializeAdapter(Context context) {
((ListActivity) context).setListAdapter(new SimpleAdapter(context, android.R.layout.simple_list_item_1, android.R.id.text1));
}
the exception is thrown here. You can get rid of the ((ListActivity) context)
, since your class already extends ListActivity
. Also this line
PinnedSectionListActivity.initializeAdapter(this);
does not make sense to me.
Upvotes: 1