Ho un listView che contiene informazioni all'interno e quando faccio clic su una riga deve darmi tutti i dettagli sotto quella riga selezionata, in quella riga selezionata ho un'immagine, nomeimmagine, prezzo, ecc così quando clicco immagine in listView deve riempire tutte le informazioni tra cui l'immagine su una prossima attivitàCome ottenere il valore suListItemClick e passarlo a un'altra attività
ho cercato di visualizzare sul mio listView e ok, qui il mio codice:
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);
}
}
MODIFICA:
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;
}
}
E posso passare la posizione alla classe successiva, ma come ottenere tutte le informazioni da una riga selezionata?
'String selectedValue = (String) getListAdapter(). GetItem (posizione);' mi dà 'NullPointerException'. È normale? – CagCak