2015-08-10 2 views
0

я хочу передачи щелкнул элемент данных из MainActivity в какой-то DetailActivity, вот код:Передача данных из ListView деятельности

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 

    Intent intent = new Intent(MainActivity.this,TourDetailActivity.class); 
    intent.putExtra("tourId", tours.get(position).getId());  
    startActivity(intent); 
    } 

здесь адаптер:

public class TourListAdapter extends ArrayAdapter<Tour> { 

Context context; 
List<Tour> tours; 

public TourListAdapter(Context context, List<Tour> tours) { 
    super(context, android.R.id.content, tours); 
    this.context = context; 
    this.tours = tours; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = vi.inflate(R.layout.list_item_tour, null); 

    Tour tour = tours.get(position); 

    TextView tv = (TextView) view.findViewById(R.id.titleText); 
    tv.setText(tour.getTitle()); 

    tv = (TextView) view.findViewById(R.id.priceText); 
    NumberFormat nf = NumberFormat.getCurrencyInstance(); 
    tv.setText(nf.format(tour.getPrice())); 

    ImageView iv = (ImageView) view.findViewById(R.id.imageView1); 
    int res = context.getResources().getIdentifier(
      tour.getImage(), "drawable", context.getPackageName()); 
    if (res != 0) { 
     iv.setImageResource(res); 
    } 
    return view; 
} 

} 

я знаю, что проблема tours.get(position).getId(). но я не знаю, как это исправить.
Спасибо

+1

туры Что это ?! можете ли вы отправить больше кода? – SaNtoRiaN

+0

поместите свой «класс адаптера». Используете ли вы «Пользовательский адаптер»? – Rustam

+0

Код не ясн. Что такое туры? – surhidamatya

ответ

0

Если вы хотите передать данные объекта между двумя действиями с помощью намерения. Вы должны использовать parcelable или Serializable.I сделал этот образец, используя Serializable. Пожалуйста, попробуйте этот.

Tour Пользовательский класс объекта будет.

public class Tour implements Serializable{ 
String title; 
String price; 
int Image; 

public String getTitle() { 
    return title; 
} 

public int getImage() { 
    return Image; 
} 

public void setImage(int image) { 
    Image = image; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getPrice() { 
    return price; 
} 

public void setPrice(String price) { 
    this.price = price; 
} 

}

и адаптер массив будет

public class Adapter extends ArrayAdapter<Tour> { 

Context context; 
List<Tour> tours; 

public Adapter(Context context, List<Tour> tours) { 
    super(context,R.layout.list_item_tour, tours); 
    this.context = context; 
    this.tours = tours; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = vi.inflate(R.layout.list_item_tour, null); 

    Tour tour = tours.get(position); 

    TextView tv = (TextView) view.findViewById(R.id.titleText); 
    tv.setText(tour.getTitle()); 

    tv = (TextView) view.findViewById(R.id.priceText); 
    /* NumberFormat nf = NumberFormat.getCurrencyInstance(); 
    tv.setText(nf.format(tour.getPrice())); 
    */ 
    ImageView iv = (ImageView) view.findViewById(R.id.imageView1); 
     /*int res = context.getResources().getIdentifier(
      tour.getImage(), "drawable", context.getPackageName()); 
    if (res != 0) { 
     iv.setImageResource(res); 
     }*/ 
     return view; 
     } 

    } 

И в вашей первой активности в MainActivity то есть вставить этот код.

Adapter adapter=new Adapter(this,tours); 
    listvView.setAdapter(adapter); 
    listvView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      Tour tour=(Tour)listvView.getAdapter().getItem(i); 
      Intent intent = new Intent(Demo.this,Demo2.class); 
      intent.putExtra("tourId",tour); 
      startActivity(intent); 
     } 
    }); 

и в вашей второй деятельности (т.е. DetailActivity) OnCreate вставить этот код

Tour objects=(Tour)getIntent().getSerializableExtra("tourId"); 
    Log.e("Selected Tour title is ",objects.getTitle()); 
Смежные вопросы