扩展MapActivity时不显示列表

时间:2011-11-29 09:04:01

标签: android listview android-arrayadapter

我在显示列表时遇到问题。

我有一个扩展MapActivity的活动,里面有一个listView对象。当活动延伸ListActivity时,一切正常。但是当它延伸MapActivity时,ListView未显示。我发现,getView()没有被调用。也许你们中的任何人都可以看看这个并告诉我我做错了什么。

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.event_list);

    // lock the screen orientation
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    eventList = EventHandler.getInstance(this).getEventList();

    adapter = new EventAdapter(this, R.layout.event_item,
            R.id.event_item_title, eventList);

    listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            BLAEvent selectedEvent = eventList.get(position);

            if (selectedEvent.getType() == BLAEvent.TYPE_MESSAGE)
                Utils.showCustomMessageDialog((Activity) getBaseContext(),
                        selectedEvent);
            else {
                Intent intent = new Intent();

                intent.putExtra("currentEventId", eventList.get(position)
                        .getId());


                setResult(Activity.RESULT_OK, intent);

                finish();


            }

        }
    });


    // refreshList
    viewLog = new Runnable() {

        @Override
        public void run() {

            getEventList();
            handler.postDelayed(viewLog, 3000);
        }
    };
    handler.post(viewLog);

}

/** The return res. */
private final Runnable returnRes = new Runnable() {

    @Override
    public void run() {

        Log.d("EventListSize in returnRes", "" + eventList.size());
        if (eventList != null && eventList.size() > 0) {
            adapter.notifyDataSetChanged();
            adapter.clear();
            for (int i = 0; i < eventList.size(); i++)
                adapter.add(eventList.get(i));
        }
        adapter.notifyDataSetChanged();
    }
};

/**
 * Gets the log.
 * 
 * @return the log
 */
private void getEventList() {

    try {
        eventList.clear();

        switch (currentEventFilter) {
        case 1:// EVENTS
            eventList.addAll(EventHandler.getInstance(getBaseContext())
                    .getEventList(BLAEvent.TYPE_REPORT));

            eventList.addAll(EventHandler.getInstance(getBaseContext())
                    .getEventList(BLAEvent.TYPE_ALERT));
            break;

        case 2:// REQUEST
            eventList.addAll(EventHandler.getInstance(getBaseContext())
                    .getEventList(BLAEvent.TYPE_REQUEST));
            break;

        case 3:// MESSAGES
            eventList.addAll(EventHandler.getInstance(getBaseContext())
                    .getEventList(BLAEvent.TYPE_MESSAGE));
            break;

        case 4: // ALL
            eventList.addAll(EventHandler.getInstance(getBaseContext())
                    .getEventList());
            break;

        default:
            eventList.addAll(EventHandler.getInstance(getBaseContext())
                    .getEventList());

            break;
        }
        Log.d("EventListSize", "" + eventList.size());
        Log.i("REFREHS LIST", "Refreshing list...");
    } catch (Exception e) {
        Log.e("BACKGROUND_PROC", e.getMessage());
    }
    runOnUiThread(returnRes);
}

这是我的XML。

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:background="@drawable/background_logo">

<RelativeLayout
    android:id="@+id/RelativeLayout01"
    android:padding="15dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button
        style="@style/ButtonGeneric.Short"
        android:visibility="gone"></Button>
    <Button
        style="@style/ButtonGeneric.Long"
        android:layout_alignParentRight="true"
        android:id="@+id/eventlist_button_newreport"
        android:onClick="onClickNewReport" android:layout_width="match_parent"></Button>



</RelativeLayout>
<TextView
        android:id="@+id/eventlist_filtername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Showing all items"
        android:textAppearance="?android:attr/textAppearanceMedium" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:text="@string/eventlist_no_reports" />
 </LinearLayout>

1 个答案:

答案 0 :(得分:0)

MapActivity用于显示地图。

ListActivity用于显示列表。

它们是不同的东西,不要混淆它们。