How to call getIntent() in adapter class

て烟熏妆下的殇ゞ 提交于 2021-01-27 14:08:47

问题


In getView() method I want to call getIntent(). How can I achieve this without starting a new activity. The getView method like this

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:


Here is the solution of this problem.

            Intent intent = ((Activity) context).getIntent();
            intent.putExtra("SELECTED_PAYMENT", mCurrentlyCheckedRB
                    .getText().toString());
            ((Activity) context).setResult(((Activity) context).RESULT_OK,
                    intent);
            ((Activity) context).finish();



回答2:


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
    }

Create Object of your Adapter class using 2nd constructor in your activity

Intent yourIntent = new Intent(); 

Or:

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



回答3:


In adapter class you pass the activity instance and catch it with a Context variable. Below snippet will help you,

private Context mcontext;

private Intent adapintent;

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

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

}



回答4:


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

PaymentData rowItem = getItem(position);


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

{

//you can include this :context.getintent(); }



来源:https://stackoverflow.com/questions/16142773/how-to-call-getintent-in-adapter-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!