2017-08-13 53 views
0

我有列表數據,然後我希望當我點擊一個項目時它會顯示細節,但我不知道如何將數據傳遞給細節活動。獲取詳細信息數據從Android工作室上的適配器

那麼,如何做到這一點?我已經嘗試了堆棧溢出的可用答案的很多方法,但它們都不起作用,或者與我的問題沒有關係。

所以,請幫助做到這一點。 I want pass data to Detail Voucher activity

這裏我的適配器腳本:

public class VoucherAdapter extends ArrayAdapter<Voucher> { 
    List<Voucher> mVoucherList; 
    LayoutInflater vi; 
    int Resource; 
    VoucherAdapter.ViewHolder holder; 
    private Context context; 


public VoucherAdapter(Context context, int resource, List<Voucher> objects) { 
    super(context, resource, objects); 
    vi = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    Resource = resource; 
    mVoucherList = objects; 
    this.context = context; 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // convert view = design 
    View v = convertView; 
    if (v == null) { 
     holder = new VoucherAdapter.ViewHolder(); 
     v = vi.inflate(R.layout.list_voucher, parent, false); 
     holder.kode = (TextView) v.findViewById(R.id.txtkode); 
     holder.status = (TextView) v.findViewById(R.id.txt_stat); 
     holder.expiredDate = (TextView) v.findViewById(R.id.txt_tgl_exp); 
     holder.list = (RelativeLayout) v.findViewById(R.id.itemListVoucher); 
     v.setTag(holder); 
    } else { 
     holder = (VoucherAdapter.ViewHolder) v.getTag(); 
    } 
    holder.kode.setText(mVoucherList.get(position).getSerialNumber()); 
    holder.status.setText(mVoucherList.get(position).getStatus()); 
    DateFormat df = new SimpleDateFormat("MMM dd yyyy HH:mma"); 
    String reportDate = df.format(mVoucherList.get(position).getExpiryDate()); 
    holder.expiredDate.setText(reportDate); 
      holder.list.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        Intent detailIntent = new Intent(getContext(), VoucherDetailAktif.class); 
        context.startActivity(detailIntent); 
        //how to pass the data to detail activity? 
       } 
      }); 

    return v; 

} 

static class ViewHolder { 

    public TextView kode; 
    public TextView expiredDate; 
    public TextView status; 
    public RelativeLayout list; 
} 
} 

謝謝

+0

拼寫錯誤,格式和大小 – user7294900

回答

0

檢查您的自定義模型實現Parcelable,如果沒有實現你的類作爲Parcelable,併發送這樣的,

Intent detailIntent = new Intent(getContext(), VoucherDetailAktif.class); 
Voucher vouceher = mVoucherList.get(position); 
detailIntent.putExtra("voucher", vouceher) 
context.startActivity(detailIntent); 

在你的VoucherDetailAktif.java活動中,你可以得到Voucher這樣的對象,

Voucher vouceher = getIntent().getParcelableExtra("voucher"); 

如果您的自定義類實現Serializable, 用於發送數據,你可以做上述相同,但在VoucherDetailAktif.java類檢索數據,你可以像這樣,

Voucher vouceherValue = (Voucher) getIntent().getSerializableExtra("voucher"); 

注:

在getView中添加最終位置參數以訪問 onClickListener

public View getView(final int position, View convertView, ViewGroup parent)