Android:自定义ListView中的错误ClassCastException

时间:2014-03-26 10:00:47

标签: android listview classcastexception

我正在尝试创建一个使用Listview实现自定义Headers的应用。但是我收到错误ClassCastException。这是我的日志和代码:

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)

这是我的代码:

主要

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) {
}

}

编辑:

我想从MainActivity调用

2 个答案:

答案 0 :(得分:1)

@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));

}

此处抛出异常。您可以删除((ListActivity) context),因为您的课程已经扩展ListActivity。也是这一行

PinnedSectionListActivity.initializeAdapter(this);

对我来说没有意义。

答案 1 :(得分:0)

简单活动中没有附加ListView,您需要使用MainActivity而不是ListActivity来扩展Activity

public class MainActivity extends ListActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

  PinnedSectionListActivity.initializeAdapter(this);
 }
}

在布局中定义您的列表,如

  <ListView
        android:id="@android:id/list"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
   </ListView>
必须使用ID android:id="@android:id/list"

定义

列表