尝试滚动列表视图时重新启动应用程序

时间:2012-07-21 13:00:34

标签: android listview

我正在开发使用ListActivity的程序。因此,当我将listview滚动到down时,它会导致重新启动应用程序并从第一个活动开始。我的代码如下:

package com.daarkoob.food;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Window;

import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FoodListActivity extends ListActivity {
    private ArrayList<Food> M_foods = new ArrayList<FoodListActivity.Food>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.foodlist);

        SQLiteDatabase sqliteDatabase = SQLiteDatabase.openOrCreateDatabase(
                "/data/data/" + this.getPackageName() + "/data.db", null);

        Cursor cursor = sqliteDatabase.rawQuery("SELECT * FROM FOOD", null);
        cursor.moveToFirst();

        while (!cursor.isAfterLast()) {
            M_foods.add(new Food(cursor.getString(2), cursor.getString(1)));
            cursor.moveToNext();
        }

        FoodAdapter adapter = new FoodAdapter(this);
        setListAdapter(adapter);

        cursor.close();
        sqliteDatabase.close();
    }

    public class Food {
        public Food(String foodName, String foodType) {
            this.M_foodName = foodName;
            this.M_foodType = foodType;
        }

        String M_foodName;
        String M_foodType;
    }

    public class FoodAdapter extends ArrayAdapter<Food> {
        private Context M_context = null;

        public FoodAdapter(Context context) {
            super(context, R.layout.foodlist_view_item, M_foods);
            this.M_context = context;
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) M_context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = layoutInflater.inflate(
                        R.layout.foodlist_view_item, parent, false);
            }
            TextView textView = (TextView) convertView
                    .findViewById(R.id.foodlist_view_item_textview);

            textView.setText(M_foods.get(position).M_foodName);

            return convertView;
        }
    }

}

foodlist_view_item.xml是:

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

    <TextView
        android:id="@+id/foodlist_view_item_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="right|center_vertical"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/black"
        android:textStyle="bold" />

</RelativeLayout>

foodlist.xml如下:

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

    <TextView
        android:id="@+id/foodlist_title_bar"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:background="@drawable/textlines"
        android:gravity="right|center_vertical"
        android:text="@string/foodlist_titlebar_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="@android:color/transparent"
        android:dividerHeight="1dip" >
    </ListView>

</LinearLayout>

出了什么问题?为什么要重启?

提前致谢:)

0 个答案:

没有答案