1

我创建了 2 个不同的活动来显示具有不同结果的 JSON 数组并将其显示到列表视图中。

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

这是我的列表视图占位符(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 上显示 JSON 数组的WeeklyFlashActivity活动(所有活动基本上彼此相似,只是获取 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();
            }
        });

    }
}
4

0 回答 0