2013-04-22 25 views
2

在getView()方法中,我想調用getIntent()。如何在不開始新活動的情況下實現這一目標。該getView方法是這樣如何在適配器類中調用getIntent()

public View getView(final int position, View convertView, ViewGroup parent) { 
    PaymentData rowItem = getItem(position); 
    LayoutInflater mInflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) { 
     convertView = mInflater.inflate(
       com.android.paypal.homeactivity.R.layout.list_item, null); 
     holder = new ViewHolder(); 
     holder.radioBtn = (RadioButton) convertView 
       .findViewById(com.android.paypal.homeactivity.R.id.rdb_payment_method); 
     convertView.setTag(holder); 
    } else 
     holder = (ViewHolder) convertView.getTag(); 

    if (position == getCount() - 1 && userSelected == false) { 
     holder.radioBtn.setChecked(true); 
     mCurrentlyCheckedRB = holder.radioBtn; 
    } else { 
     holder.radioBtn.setChecked(false); 
    } 

    holder.radioBtn.setText(rowItem.getRdbText()); 
    return convertView; 
} 
+1

爲什麼需要它?原因? – Sajmon 2013-04-22 08:42:49

+0

您可以將數據傳遞給適配器的構造函數。 – Raghunandan 2013-04-22 08:47:15

+0

其實當用戶點擊當前按鈕時,我想要傳遞數據另一個活動,而不創建新的活動。 – androidcodehunter 2013-04-22 08:47:48

回答

11

這是這個問題的解決方案。

  Intent intent = ((Activity) context).getIntent(); 
      intent.putExtra("SELECTED_PAYMENT", mCurrentlyCheckedRB 
        .getText().toString()); 
      ((Activity) context).setResult(((Activity) context).RESULT_OK, 
        intent); 
      ((Activity) context).finish(); 
3
public class MyAdapter extends ArrayAdapter 
{ 
    private Context context; 
    private Intent intent; 

    MyAdapter(Context context) 
    { 
     this.context = context; 
    } 

    MyAdapter(Context context,Intent intent) 
    { 
     this(context); 
     this.intent = intent; // use this intent 
    } 

    private View getView() 
    { 
     // use intent here 
    } 

在您的活動創建一個使用第2構造適配器類的對象

Intent yourIntent = new Intent(); 

或者:

Intent yourIntent = getIntent(); 
MyAdapter adapter = new MyAdapter(context,yourIntent); // here pass intent 
+0

這是什麼? – Sajmon 2013-04-22 08:43:59

-1

公共查看getView(最終詮釋位置,View convertView,ViewGroup parent){

PaymentData rowItem = getItem(position); 


LayoutInflater mInflater = (LayoutInflater) context 
     .getSystemService(Context.getintent().LAYOUT_INFLATER_SERVICE); 

{

//你可以包括這樣的:context.getintent(); }

0

在適配器類中,您傳遞活動實例並使用Context變量來捕獲它。 下面的代碼段將幫助你,

private Context mcontext;

private Intent adapintent; 

MyIntentAdapter(Context context){ 
    this.mcontext = context; 
} 

MyIntentAdapter(Context context,Intent intent){ 
    this(context); 
    this.adapintent= intent; 

} 
相關問題