Jin-guk Park
Jin-guk Park

Reputation: 241

How to return value in ListViewAdapter

I started the Activity named CarrierSelectActivity using startActivityForResult method. and the activity has a ListView which is binded to CarrierSelectListAdapter.

I thought that just can solve using context.setResult(intent); but the context didn't have setResult() method

This is getView() in CarrierSelectListAdapter.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final int pos = position;
    final Context context = parent.getContext();

    // "listview_item" Layout을 inflate하여 convertView 참조 획득.
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_select_carriers, null);
    }

    // 화면에 표시될 View(Layout이 inflate된)으로부터 위젯에 대한 참조 획득
    ConstraintLayout layout = convertView.findViewById(R.id.item_select_carrier);
    ImageView logo = convertView.findViewById(R.id.item_select_carriers_logo);
    TextView name = convertView.findViewById(R.id.item_select_carriers_carrierName);



    // Data Set(listViewItemList)에서 position에 위치한 데이터 참조 획득
    Carrier carrier = listViewItemList.get(position);

    // 아이템 내 각 위젯에 데이터 반영
    logo.setImageDrawable(context.getDrawable(context.getResources().getIdentifier(carrier.getLogo(), "drawable", context.getPackageName())));
    name.setText(carrier.getName());

    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("carrierName",);
            intent.putExtra("code");
            context.set         //HERE!!!!!!!!!!!!
        }
    });


    return convertView;
}

Upvotes: 0

Views: 37

Answers (1)

LarsH
LarsH

Reputation: 27994

setResult() is a method of Activity, not of Context. There are a couple of ways to get the Activity.

  • Often, the ListAdapter is an inner class of an Activity, so it has access to Activity.this. Then you can call Activity.this.setResult(...). So in your case, CarrierSelectListAdapter could be an inner class of CarrierSelectActivity, and your onClickListener could call CarrierSelectActivity.this.setResult(...).

  • Alternatively, you could pass the activity to the constructor of CarrierSelectListAdapter, which could assign it to a member variable mActivity. Then when you need to set the result, call mActivity.setResult(...).

Upvotes: 1

Related Questions