0

私には2つの活動があります。最初のアクティビティはサーバーからのリストであり、2番目のアクティビティもリストビューです。最初のアクティビティでonItemClickを実行すると、2番目のアクティビティに最初のアクティビティで選択したアイテムのリストが表示されます。これが私のコードです:

-最初の活動-

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);
                }
            });
        }
    }
}

-2番目のアクティビティ-

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);
    }
}

2番目のアクティビティでまだリストを表示できません。私のコードを修正してください、そして私はあなたの提案が必要です。

4

0 に答える 0