我是Android开发人员的新手,我的自定义列表视图中存在问题,条件是当我点击我的列表视图时应该转到详细活动。是的,它的确有效! 但细节数据和图像不会出现。只有 detail.xml ,没有数据和图片。
Ymainactivity.java
<div id="grid1" ui-grid="gridOptions" class="grid" ui-grid-grouping></div>
DetailActivity.java
private class GetData extends AsyncTask<String, Void, String> {
JSONArray str_json = null;
JSONObject json = null;
JSONParser jParser = new JSONParser();
ListView listx;
LazyAdapter adapter;
ArrayList<HashMap<String, String>> data_map = new ArrayList<HashMap<String, String>>();
ProgressDialog dialog = new ProgressDialog(YmainActivity.this);
protected void onPreExecute() {
super.onPreExecute();
this.dialog.setMessage("Memuat Item..");
this.dialog.show();
}
protected String doInBackground(String... param) {
json = jParser.AmbilJson(link_url);
try {
str_json = json.getJSONArray("berita");
for(int i = 0; i < str_json.length(); i++){
JSONObject ar = str_json.getJSONObject(i);
String kodebrg = "kode barang : "+ar.getString("kodebrg");
String gambar = ar.getString("gambar2");
String nama = ar.getString("nama");
String stok = "Stok : "+ar.getString("stok")+" "+ar.getString("satauan");
String harga = "Rp. "+ar.getString("harga")+" per "+ar.getString("satauan");
String info = ar.getString("info");
HashMap<String, String> map = new HashMap<String, String>();
map.put(in_nama, nama);
map.put(in_stok, stok);
map.put(in_kodebrg, kodebrg);
map.put(in_gambar, gambar);
map.put(in_harga, harga);
map.put(in_info, info);
data_map.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
dialog.dismiss();
listx = (ListView)findViewById(R.id.listos);
adapter = new LazyAdapter(YmainActivity.this, data_map);
listx.setAdapter(adapter);
if (adapter.getCount()==0) {
Toast.makeText(YmainActivity.this,"data tidak ditemukan",Toast.LENGTH_SHORT).show();
}
listx.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
HashMap<String, String> map = (HashMap<String, String>) adapter.getItem(position);
String kodebrgs = ((TextView) view.findViewById(R.id.kodebrgs)).getText().toString();
Intent inx = new Intent(YmainActivity.this, DetailActivity.class);
inx.putExtra(kodebrgs, map2.get(in_kodebrg));
startActivity(inx);
}
LazyAdapter.java
private class GetData extends AsyncTask<String, Void, String> {
JSONArray artikel = null;
JSONObject json = null;
JSONParser jParser = new JSONParser();
Intent ins = getIntent();
String kode1s = ins.getStringExtra(in_kodebrg);
String link_url = "http:// my php file that call all data using primary id from table in my database mySQL"+kode1s;
ProgressDialog dialog = new ProgressDialog(DetailActivity.this);
protected void onPreExecute() {
super.onPreExecute();
this.dialog.setMessage("Memuat Detail Produk..");
this.dialog.show();
}
protected String doInBackground(String... params) {
json = jParser.AmbilJson(link_url);
return null;
}
protected void onPostExecute(String result) {
dialog.dismiss();
try {
artikel = json.getJSONArray("artikel");
for(int i = 0; i < artikel.length(); i++){
JSONObject ar = artikel.getJSONObject(i);
TextView judul1 = (TextView) findViewById(R.id.judul2);
TextView detail1 = (TextView) findViewById(R.id.detail2);
TextView isi1 = (TextView) findViewById(R.id.isi2);
TextView info1 = (TextView) findViewById(R.id.infobarang2);
TextView kodebarang1 = (TextView) findViewById(R.id.kodenyabrg2);
String judul_1 = ar.getString("nama");
String detail_1 = "harga Rp. "+ ar.getString("harga");
String isi_1 = "Stok Barang : "+ ar.getString("stok")+" "+ar.getString("satauan");
String info_1 = "Info : "+ar.getString("info");
String kodebarang_1 = "Kode Barang : "+ar.getString(in_kodebrg);
judul1.setText(judul_1);
detail1.setText(detail_1);
isi1.setText(isi_1);
info1.setText(info_1);
kodebarang1.setText(kodebarang_1);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这是我自定义的listiview pict
http://cdn.gudangimages.com/v1/2015/06/05/gambarlistview.png
这是详细的xml,当我点击我的自定义列表视图中的一个列表时,结果为空,没有数据和图像。
http://cdn.gudangimages.com/v1/2015/06/05/gambardetail.png
我不知道代码有什么问题,因为当我运行该程序时,它并没有强行关闭。
答案 0 :(得分:0)
我认为问题出在inx.putExtra(kodebrgs, map2.get(in_kodebrg));
你应该使用不会改变的密钥......
Intent i = new Intent(<currentActivity>.this,
<newactivity>.class);
i.putExtra("ticket", ticket);
i.putExtra("ticketid", tid);
startActivity(i);
以及其他意图:
String ticketid = getIntent().getStringExtra("ticketid").toString();
String ticket = getIntent().getStringExtra("ticket").toString();
你也可以用不同的密钥发送每个字符串,并在密钥的帮助下获取详细信息。
答案 1 :(得分:0)