android列表加载另一个列表

时间:2012-09-25 07:14:26

标签: android android-intent android-listview android-arrayadapter

我有两项活动。第一个活动是来自服务器的列表,第二个活动也是列表视图。如果我在第一个活动中执行onItemClick,则第二个活动将显示第一个活动中所选项目的列表。这是我的代码:

- 第一项活动 -

package com.statis.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import com.example.main.DetailCategoryActivity;
import com.example.main.DetailRestoActivity;
import com.example.main.R;
import com.example.main.ResultRestoActivity;
import com.example.main.TabResultRestoActivity;
import com.example.parser.JSONParser;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class List_ATM extends ListActivity {
    //Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
    JSONArray data = null;

    ArrayList<HashMap<String,String>> dataList;

    //url
    private static String url_ = "http://www.tw-indo.com/droid_cari/atm.php";

 // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_DATA = "Vw_Merchant_Detail";
    private static final String TAG_Pk_Merchant_ID = "Pk_Merchant_ID";
    private static final String TAG_DataLevel1_ID = "DataLevel1_ID";
    private static final String TAG_DataLevel2_ID = "DataLevel2_ID";
    private static final String TAG_DataLevel1 = "DataLevel1";
    private static final String TAG_DataLevel2 = "DataLevel2";
    private static final String TAG_DataLevel3 = "DataLevel3";
    private static final String TAG_Address = "Address";
    private static final String TAG_Longitude = "Longitude";
    private static final String TAG_Latitude = "Latitude";

    Context ctx;

    String kota,subtitles;
    String Pk_Merchant_ID,DataLevel1_ID,DataLevel2_ID,  
    DataLevel1, DataLevel2, DataLevel3, Address, 
    Longitude, Latitude ;

    TextView selection, tvArea, tvRestoIndo, tvJenisMerchant;

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        kota = getIntent().getStringExtra("cities");
        subtitles = getIntent().getStringExtra("subtitle");
        ctx = this;
        setContentView(R.layout.activity_detailcategory_list);

        selection = (TextView)findViewById(R.id.tv_detailrestaurant);

        tvJenisMerchant = (TextView)findViewById(R.id.tvRestoran);
        tvJenisMerchant.setText(""+subtitles+"");

        tvArea = (TextView)findViewById(R.id.area);
        tvArea.setText(""+kota+"");

        tvRestoIndo = (TextView)findViewById(R.id.tvRestoranIndo);
        tvRestoIndo.setText(""+kota+"");

        dataList = new ArrayList<HashMap<String, String>>();
        new LoadData().execute();

        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                    long arg3) {
                // TODO Auto-generated method stub
                DataLevel2_ID = ((TextView) v.findViewById(R.id.id_merchant)).getText()
                                .toString();
                Address = ((TextView) v.findViewById(R.id.tv_detailrestaurant)).getText()
                                .toString();
                Intent i = new Intent(getApplicationContext(), ResultRestoActivity.class);
                    i.putExtra(TAG_DataLevel2_ID, DataLevel2_ID);
                    i.putExtra(TAG_Address, Address);
                startActivity(i);
            }
        });
    }

    class LoadData extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(List_ATM.this);
            pDialog.setMessage("Loading Merchant...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            JSONObject json = jParser.makeHttpRequest(url_, "GET", params);
            Log.d("Get List :", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    data = json.getJSONArray(TAG_DATA);
                    for (int i=0; i<data.length(); i++) {
                        JSONObject c = data.getJSONObject(i);
                        String Pk_Merchant_ID = c.getString(TAG_Pk_Merchant_ID);
                        String DataLevel1_ID = c.getString(TAG_DataLevel1_ID);
                        String DataLevel2_ID = c.getString(TAG_DataLevel2_ID);
                        String DataLevel1 = c.getString(TAG_DataLevel1);
                        String DataLevel2 = c.getString(TAG_DataLevel2);
                        String DataLevel3 = c.getString(TAG_DataLevel3);
                        String Address = c.getString(TAG_Address);
                        String Longitude = c.getString(TAG_Longitude);
                        String Latitude = c.getString(TAG_Latitude);

                        HashMap<String, String> map = new HashMap<String, String>();
                            map.put(TAG_Pk_Merchant_ID, Pk_Merchant_ID);
                            map.put(TAG_DataLevel1_ID, DataLevel1_ID);
                            map.put(TAG_DataLevel2_ID, DataLevel2_ID);
                            map.put(TAG_DataLevel1, DataLevel1);
                            map.put(TAG_DataLevel2, DataLevel2);
                            map.put(TAG_DataLevel3, DataLevel3);
                            map.put(TAG_Address, Address);
                            map.put(TAG_Longitude, Longitude);
                            map.put(TAG_Latitude, Latitude);
                        dataList.add(map);
                    }
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String file_url) {
            pDialog.dismiss();

            runOnUiThread(new Runnable() {
                public void run() {
                    // TODO Auto-generated method stub
                    ListAdapter adapter = new SimpleAdapter(
                            List_ATM.this, dataList, 
                            R.layout.activity_detailcategory, new String[] {TAG_DataLevel2_ID, TAG_DataLevel2},
                            new int[] {R.id.id_merchant, R.id.tv_detailrestaurant});
                    setListAdapter(adapter);
                }
            });
        }
    }
}

- 第二次活动 -

package com.example.main;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import com.example.main.R.layout;
import com.statis.activity.List_ATM;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class ResultRestoActivity extends Activity {

    Context ctx;

    String[] id, address;

    TextView tes,tes2;

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getIntent().getExtras().getStringArray("DataLevel2_ID");
        address = getIntent().getExtras().getStringArray("Address");
        ctx = this;
        setContentView(R.layout.activity_restoresult);

        ListView listresto = (ListView)findViewById(R.id.list);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        for (int i=0; i<mylist.size(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
            map.put("id", (String) Array.get(id, i));
            map.put("atm", "BCA");
            map.put("alamat", (String) Array.get(address, i));
        mylist.add(map);
        }

        SimpleAdapter sAdapter = new SimpleAdapter(ctx, mylist, R.layout.restoresult_row, 
                new String[] {"id","alamat"}, new int[] {R.id.merchant_name, R.id.merchant_address} );

        listresto.setAdapter(sAdapter);
    }
}

我仍然无法在第二个活动中显示列表。请更正我的代码,我需要你的建议。

0 个答案:

没有答案