0

我有这段代码通过 php 代码从数据库中检索列表项

public class MainActivity extends Activity implements OnItemClickListener {
    // Progress Dialog 
    private ProgressDialog pDialog; 
    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
    ArrayList<HashMap<String, String>> DaftarRS; 
    // url daftar rumah sakit 
    private static String url_daftar_rs = "http://www.iagstore.net/nangnur/daftar_rumah_sakit.php"; 
    // JSON Node names 
    public static final String TAG_SUCCESS = "success";
    public static final String TAG_DAFTAR_RS = "daftar_rs"; 
    public static final String TAG_ID_RS = "id_rs"; 
    public static final String TAG_NAMA_RS = "nama_rs"; 
    public static final String TAG_LINK_IMAGE_RS = "link_image_rs";
    public static final String TAG_ALAMAT_RS = "alamat_rs";
    public static final String TAG_TELEPON_RS = "telepon_rs"; 
    Button ButttonInputRumahSakit; 
    // daftar_rs JSONArray 
    JSONArray daftar_rs = null; ListView list; ListAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Hashmap for ListView
        DaftarRS = new ArrayList<HashMap<String, String>>();
        // Loading daftar rumah sakit in Background Thread
        new LoadDaftarRumahSakitActivity().execute();
        // Get listview
        list = (ListView) findViewById(R.id.list);
        list.setOnItemClickListener(this);

    }

    /** * Updating parsed JSON data into ListView * */
    public void SetListViewAdapter(ArrayList<HashMap<String, String>> daftar) {
        adapter = new ListAdapter(this, daftar);
        list.setAdapter(adapter);
    }

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { super.onActivityResult(requestCode, resultCode, data);
    // if result code 100 
    if (resultCode == 100) { 
        // if result code 100 is received // means user edited/deleted record
        // reload this screen again 
        Intent intent = getIntent(); finish(); 
        startActivity(intent); } } 
    /** * Background Async Task to Load all record data by making HTTP Request * */
    class LoadDaftarRumahSakitActivity extends AsyncTask<String, String, String> {

        /** * Before starting background thread Show Progress Dialog * */
        @Override protected void onPreExecute() { super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Mohon tunggu...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show(); }
        /** * getting All record data from url * */ 
        protected String doInBackground(String... args) { 
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>(); 
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_daftar_rs, "GET", params);
            // Check your log cat for JSON reponse
            Log.d("Daftar Rumah Sakit: ", json.toString()); try {
                // Checking for SUCCESS TAG 
                int success = json.getInt(TAG_SUCCESS); 
                if (success == 1) { 
                    // Ada record Data (SUCCESS = 1) 
                    // Getting Array of daftar_rs 
                    daftar_rs = json.getJSONArray(TAG_DAFTAR_RS);
                    // looping through All daftar_rs 
                    for (int i = 0; i < daftar_rs.length(); i++) { JSONObject c = daftar_rs.getJSONObject(i); 
                    // Storing each json item in variable
                    String id_rs = c.getString(TAG_ID_RS); 
                    String nama_rs = c.getString(TAG_NAMA_RS); 
                    String link_image_rs = c.getString(TAG_LINK_IMAGE_RS);
                    String alamat_rs = c.getString(TAG_ALAMAT_RS); 
                    String telepon_rs = c.getString(TAG_TELEPON_RS);
                    // creating new HashMap 
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID_RS, id_rs); map.put(TAG_NAMA_RS, nama_rs); 
                    map.put(TAG_LINK_IMAGE_RS, link_image_rs); 
                    map.put(TAG_ALAMAT_RS, alamat_rs);
                    map.put(TAG_TELEPON_RS, telepon_rs);
                    // adding HashList to ArrayList 
                    DaftarRS.add(map);
} } else {
    // Tidak Ada Record Data (SUCCESS = 0) 
    // Akan menutup aplikasi 
    finish(); } } catch (JSONException e) { e.printStackTrace(); } return null;
    } 
        /** * After completing background task Dismiss the progress dialog * **/ 
        protected void onPostExecute(String file_url) { 
            // dismiss the dialog after getting all record rumah sakit
            pDialog.dismiss(); // updating UI from Background Thread 
            runOnUiThread(new Runnable() { public void run() { 
                // updating listview 
                SetListViewAdapter(DaftarRS); 
            } }); } }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // Cant Figure it out!!!!!!!!!!!
        ??????
                ???????
                 ??????

    } }

你能指导我如何获得列表位置并传递给另一个活动吗?我知道我应该使用 putExtra 方法,但我已经使用它并且总是将第一项传递给下一个活动

4

0 回答 0