切换活动时为什么我的listview为空?

时间:2012-06-26 09:51:01

标签: android android-listview android-activity

我创建了两个不同的活动来显示具有不同结果的JSON数组并将其显示到listview中。

首先,我创建我的菜单活动,用按钮(Button1和Button2)控制开关活动 然后,我创建了2个活动类(Activity1和Activity2)以在listview中显示我的JSON数组 单击Button1时,它会在listview中的Activity1上显示我的JSON数组 但是当我点击Button2时,它什么也没显示。
为什么我的Activity2的listview项目丢失了?

这是我的listview占位符(listviewplaceholder):

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

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""/>

    <Button
        android:id="@+id/buttonClose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Close" />

</LinearLayout>

这是获取值的布局(wfid):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="7dp"
    >
<TextView  
    android:id="@+id/item_title"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:padding="2dp"
    android:textSize="20dp" />
    <TextView  
    android:id="@+id/item_subtitle"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="2dp"
    android:textSize="13dp" />

</LinearLayout>

这是我的菜单活动(MenuActivity):

public class MenuActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // TODO Auto-generated method stub
        Button btnIndonesia = (Button) findViewById(R.id.button1);
        Button btnMalaysia = (Button) findViewById(R.id.button2);
        Button btnSingapore = (Button) findViewById(R.id.button3);
        Button btnHongkong = (Button) findViewById(R.id.button4);

        //Listening to button event
        btnIndonesia.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Starting a new Intent
                Intent indonesiaScreen = new Intent(getApplicationContext(), WeeklyFlashActivity.class);

                startActivity(indonesiaScreen);

            }
        });

        btnMalaysia.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Starting a new Intent
                Intent malaysiaScreen = new Intent(getApplicationContext(), WeeklyFlashMyActivity.class);

                startActivity(malaysiaScreen);

            }
        });

        btnSingapore.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Starting a new Intent
                Intent singaporeScreen = new Intent(getApplicationContext(), WeeklyFlashSgActivity.class);

                startActivity(singaporeScreen);

            }
        });

        btnHongkong.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Starting a new Intent
                Intent hongkongScreen = new Intent(getApplicationContext(), WeeklyFlashHkActivity.class);

                startActivity(hongkongScreen);

            }
        });
    }

}

这是我在listview WeeklyFlashActivity上显示JSON数组的活动(所有活动基本上彼此相似,只有获取JSON的URL不同):

public class WeeklyFlashActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        JSONObject json = JSONfunctions.getJSONfromURL("http://www.greenfields.co.id:502/Service1.svc/json/weeklyflash/id");

        try
        {
            JSONArray  weeklyflash = json.getJSONArray("GetReportResult");

            for(int i=0;i<weeklyflash.length();i++)
            {                       
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = weeklyflash.getJSONObject(i);

                //map.put("type",  String.valueOf(i));
                map.put("type", e.getString("type"));
                map.put("total", e.getString("total"));

                mylist.add(map);            
            }       
        }
        catch(JSONException e)        
        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }

        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.wfid, 
                        new String[] { "type", "total" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle });

        setListAdapter(adapter);
        Button btnClose = (Button) findViewById(R.id.buttonClose);
        btnClose.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Closing SecondScreen Activity
                finish();
            }
        });

    }
}

0 个答案:

没有答案