ListView适配器setResult()和Finish()错误

时间:2016-10-09 11:27:08

标签: android listview baseadapter

我想通过使用startActivityForResult()方法startActivity for Result。现在开始的活动有一个带有适配器类的listview.I已经跟随Listview Adapter,所以当用户选择任何项目时它应该返回到调用活动选择项目名称。我无法调用2个方法

setResult() and finish() in following code

适配器代码:

view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            SharedPreferences prefernces = mContext.getSharedPreferences("MyKey111", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor1 = prefernces.edit();
            editor1.putString("Custname",customerpopulationlist.get(position).getName());
            editor1.putString("let_id", customerpopulationlist.get(position).getLetId());
            editor1.commit();

            Intent intentMessage = new Intent();

            // put the message in Intent
            intentMessage.putExtra("MESSAGE", "hello");
            intentMessage.putExtra("selected_refer", customerpopulationlist.get(position).getLetId());

            setResult(RESULT_OK, intentMessage);
            finish();

        }
    });

3 个答案:

答案 0 :(得分:3)

您必须持有对创建适配器的活动的引用,然后将结果设置为该引用。你正在做的事情是设置OnClickListener对象的结果!!

将一个Activity对象添加到适配器构造函数并将其保存在适配器中的局部变量中并将其命名为 act ,然后调用活动的 setResult(),如下所示:

view.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        SharedPreferences prefernces = mContext.getSharedPreferences("MyKey111", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor1 = prefernces.edit();
        editor1.putString("Custname",customerpopulationlist.get(position).getName());
        editor1.putString("let_id", customerpopulationlist.get(position).getLetId());
        editor1.commit();

        Intent intentMessage = new Intent();

        // put the message in Intent
        intentMessage.putExtra("MESSAGE", "hello");
        intentMessage.putExtra("selected_refer", customerpopulationlist.get(position).getLetId());

        //THESE TWO LINES NEED TO BE CHANGED
        act.setResult(RESULT_OK, intentMessage);
        act.finish();

    }
});

答案 1 :(得分:1)

您需要返回的活动需要override方法protected void onActivityResult(int requestCode, int resultCode, intent data,即您呼叫startActivityforResult()的活动。

  @override
    protected void onActivityResult(int requestCode, int resultCode, intent data){
    if(resultCode == -1) // -1 for RESULT_OK
    { // your logic goes here...
    }
    }

答案 2 :(得分:0)

Activity对象和Context对象之间是有区别的(如果有混淆,请更好地阅读文档以更好地理解)。

当您要进行基于活动的操作时,请遵循以下步骤

Activity activity = this; 
Context context = ActivityName.this;

在适配器中传递两个对象,然后使用

activity.setResult(Activity.RESULT_OK,intent); 
activity.finish();

对于基于上下文的操作,请使用Intent i = new Intent(ListViewAdapter.this.mContext,EventPrivacyAndTicketPrice.class);

查看下面的完整活动代码:

    public class ActivityX extends AppCompatActivity {
Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_x);
        intent = getIntent();
        ListView listView = findViewById(R.id.list_x);
        ArrayList<ETypeObject> allTypes= new ArrayList<>();
        allTypes = GetData();

        ListViewAdapter listViewAdapter= new       ListViewAdapter(allTypes,ActivityX.this,intent,this);
        listView.setAdapter(listViewAdapter);

    }

    public ArrayList<ETypeObject> GetData(){
        ArrayList<ETypeObject> allTypes= new ArrayList<>();
        allTypes.add(new ETypeObject("Object1",0));
        allTypes.add(new ETypeObject("Object2, Talk",0));
        allTypes.add(new ETypeObject("Object3",0));

   return allTypes;
    }


}

class ETypeObject {
   public String name;
    public int imageId;

    public ETypeObject(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }

    public ETypeObject() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}




 class ListViewAdapter extends BaseAdapter{
    Activity activity;
    Context mContext;
    ArrayList<ETypeObject> allTypes= new ArrayList<>();
    Intent intent;
    public ListViewAdapter(){}

    public ListViewAdapter(ArrayList<ETypeObject> AllTypes, Context context,Intent intent,Activity activity){
        mContext = context;
        allTypes = AllTypes;
        activity =activity;
        this.intent = intent;

    }



    @Override
    public int getCount() {
        return allTypes.size();
    }

    @Override
    public Object getItem(int i) {
        return allTypes.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i1, View view, ViewGroup viewGroup) {

        LayoutInflater layoutInflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (layoutInflater!=null){
            view = layoutInflater.inflate(R.layout.lay1,null);

        }

        TextView type;


        type = view.findViewById(R.id.parentInteresttext);

        type.setText(allTypes.get(i1).getName());

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // Toast.makeText(mContext,allTypes.get(i1).getName(),Toast.LENGTH_LONG).show();

                if (intent.getBooleanExtra("fromThirdActivty",false)){

                    Intent i = new Intent();
                    i.putExtra("eventType",allTypes.get(i1).getName());
                    activity.setResult(Activity.RESULT_OK,intent);
                    activity.finish();

                }


                Intent i = new Intent( ListViewAdapter.this.mContext,OtherActivity.class);

                i.putExtra("eventType",allTypes.get(i1).getName());
                ListViewAdapter.this.mContext.startActivity(i);
            }
        });
        return view;
    }
}