Android - 在listview项目中制作单个活动

时间:2014-08-24 20:24:37

标签: java android mysql json listview

我可以在listview中调用单个活动,我已经搜索了任何代码,但我从来不知道如何实现它,因为我在我的代码中使用JSON解析。有人可以告诉我如何使用我的代码。

这是我的MainActivity.java

public class MainActivity extends Activity {
// Progress Dialog

private ProgressDialog pDialog;
// Creating JSON Parser object 
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> DaftarRS;

private static String url_daftar_rs = "http://192.168.43.226/jualan/barang_tes/list.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";
public static final String TAG_HARGA_RS = "harga_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_load_barang);

    // Hashmap for ListView

    DaftarRS = new ArrayList<HashMap<String, String>>();
    // Loading in Background Thread
    new LoadDaftarRumahSakitActivity().execute();
    // Get listview
    list = (ListView) findViewById(R.id.list);
            // Launching new screen on Selecting Single ListItem
    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            HashMap<String, String> map = DaftarRS.get(position);

            Intent in = new Intent(MainActivity.this, SingleMenuItemActivity.class);

            in.putExtra(TAG_ID_RS, map.get(TAG_ID_RS));
            in.putExtra(TAG_NAMA_RS, map.get(TAG_NAMA_RS));                         
            in.putExtra(TAG_LINK_IMAGE_RS, map.get(TAG_LINK_IMAGE_RS));
            in.putExtra(TAG_ALAMAT_RS, map.get(TAG_ALAMAT_RS));
            in.putExtra(TAG_TELEPON_RS, map.get(TAG_TELEPON_RS));
            in.putExtra(TAG_HARGA_RS, map.get(TAG_HARGA_RS));

            startActivity(in);
        }                
    }); 
}
/**
*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);
        String harga_rs = c.getString(TAG_HARGA_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);
map.put(TAG_HARGA_RS, harga_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);
    }
});
}
}
}

这是我的SingleMenuItemActivity.java

public class SingleMenuItemActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list);

    Intent i = getIntent();
    //now your item data are in i

    String TAG_ID_RS = i.getStringExtra("TAG_ID_RS");
    String TAG_NAMA_RS = i.getStringExtra("TAG_NAMA_RS");
    String TAG_LINK_IMAGE_RS = i.getStringExtra("TAG_LINK_IMAGE_RS");
    String TAG_ALAMAT_RS = i.getStringExtra("TAG_ALAMAT_RS");
    String TAG_TELEPON_RS = i.getStringExtra("TAG_TELEPON_RS");
    String TAG_HARGA_RS = i.getStringExtra("TAG_HARGA_RS");

    TextView namaView = (TextView) findViewById(R.id.nama_rs);
    ImageView link_img = (ImageView) findViewById(R.id.image_rs);
    TextView alamat = (TextView) findViewById(R.id.alamat_rs);
    TextView telepon = (TextView) findViewById(R.id.telepon_rs);
    TextView harga = (TextView) findViewById(R.id.harga_rs);

    namaView.setText(TAG_NAMA_RS);
    //link_img.setImageBitmap(TAG_LINK_IMAGE_RS);
    alamat.setText(TAG_ALAMAT_RS);
    telepon.setText(TAG_TELEPON_RS);
    harga.setText(TAG_HARGA_RS);

}

}

1 个答案:

答案 0 :(得分:0)

在您的主要活动的onCreate方法中,在您的上一行添加:

之后
    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
            HashMap<String,String> item = (HashMap<String,String>) adapter.getItem(position);
            Intent intent = new Intent(this, YourSecondActivity.class);
            intent.putExtra("TAG_ID_RS", item.get("TAG_ID_RS"));
            intent.putExtra("TAG_NAMA_RS", item.get("TAG_NAMA_RS"));
            ......
            startActivity(intent);
        }
    });

在YourSecondActivity.java中有类似的内容:

    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yoursecondactivity);

        Intent i = getIntent();
        //now your item data are in i

        String TAG_ID_RS = i.getString("TAG_ID_RS");
        String TAG_NAMA_RS = i.getString("TAG_NAMA_RS");
        ....
        TextView namaView = (TextView)findViewById(R.id.yourNameView);
        namaView.setText(TAG_NAMA_RS);
        ....
    }

对于您的意图接收活动,您需要在清单文件中定义一个意图过滤器:         &lt; activity android:name =&#34; SingleMenuItemActivity&#34;&gt;             &LT;意图滤光器&gt;                 &lt; action android:name =&#34; yourpackage.intent.action.showsinglemenuitem&#34; /&gt;

            &LT; /意图滤光器&gt;         &LT; /活性GT;

使用&#34; yourpackage.intent.action.showsinglemenuitem&#34;构造INTENT时的动作名称为p

我想我终于找到了你的问题:

在MainActivity中,您可以定义字段名称,例如:

     public static final String TAG_NAMA_RS = "nama_rs";

然后用Intent填充该名称,

但是在您的SingleMenuItemActivity中,您尝试使用不同的名称访问它们:

     String TAG_NAMA_RS = i.getStringExtra("TAG_NAMA_RS");

你需要做

     String TAG_NAMA_RS = i.getStringExtra("nama_rs");