Tôi có một listView có thông tin bên trong và khi tôi nhấp vào một hàng, nó phải cho tôi tất cả chi tiết bên dưới hàng đã chọn, trong hàng chọn đó tôi có hình ảnh, tên tưởng tượng, giá vv nên khi tôi bấm hình ảnh trong ListView nó phải cư tất cả các thông tin bao gồm các hình ảnh trên một hoạt động tiếp theoCách lấy giá trị trênListItemClick và chuyển nó sang hoạt động khác
tôi đã cố gắng để hiển thị trên ListView của tôi và nó ok, đây mã của tôi:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/php/p.php");
try{
JSONArray earthquakes = json.getJSONArray("prop");
for(int i=0;i<earthquakes.length();i++){
JSONObject e = earthquakes.getJSONObject(i);
String BookName = e.getString("P_City");
PNames.add(BookName);
String BookImg = e.getString("Pname");
PImages.add(BookImg);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
setListAdapter(new MobileArrayAdapter(this, PNames,PImages));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Searching.class);
// sending data to new activity
i.putExtra("product", selectedValue);
startActivity(i);
}
}
EDIT:
private final Context context;
private final String[] myBookNamelist = null;
private ArrayList<String> MyP = new ArrayList<String>();
private ArrayList<String> myPurl = new ArrayList<String>();
//ArrayList<HashMap<String, String>> myBookNamelist = new ArrayList<HashMap<String, String>>();
//ArrayList<HashMap<String, String>> myBookImageurl = new ArrayList<HashMap<String, String>>();
public MobileArrayAdapter(Context context,ArrayList<String> Bname,ArrayList<String> BUrl) {
super(context, R.layout.list_mobile, Bname);
this.context = context;
this.MyP = Bname;
this.myPurl = BUrl;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(MyP.get(position));
Bitmap bitmap = null;
// Change icon based on name
JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/php/p.php");
// http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
try{
JSONArray earthquakes = json.getJSONArray("prop");
//for(position=0;position<earthquakes.length();position++){
JSONObject e = earthquakes.getJSONObject(position);
String BB = e.getString("P_City");
MyP.add(BB);
String UU = e.getString("Pname");
myPurl.add(UU);
//}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
String s = MyP.get(position);
String i = myPurl.get(position);
System.out.println(s);
System.out.println(i);
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(i).getContent());
} catch (MalformedURLException err) {
// TODO Auto-generated catch block
err.printStackTrace();
} catch (IOException err) {
// TODO Auto-generated catch block
err.printStackTrace();
}
//if (!s.equals("")) {
imageView.setImageBitmap(bitmap);
//} else {
System.out.println("Bitmap image: "+position+"="+bitmap);
//imageView.setImageResource(R.drawable.android_logo);
//}
return rowView;
}
}
Và tôi có thể chuyển vị trí cho lớp tiếp theo, nhưng cách nhận tất cả thông tin từ một hàng đã chọn?
'String selectedValue = (String) getListAdapter(). GetItem (vị trí);' cho tôi 'NullPointerException'. Điều đó có bình thường không? – CagCak