当前位置:  开发笔记 > Android > 正文

适配器类中的super.onActivityResult?Android的

如何解决《适配器类中的super.onActivityResult?Android的》经验,为你挑选了1个好方法。

我有RecyclerView非常复杂的项目,多个按钮/图像/文本.要访问每个按钮/图像,我在Adapter类中设置了单击Listeners.

但现在我正在尝试添加功能来设置联系人铃声,我有一个问题.

我正在调用onActivityResult方法,如下所示:

Intent contactIntent = new Intent(Intent.ACTION_PICK);
                contactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                contactIntent.putExtra("RINGTONE_POSITION", position);
                ((Activity) context).startActivityForResult(contactIntent, CONTACT_CHOOSER_ACTIVITY_CODE );

这很好.但问题出在这里:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
....
...
...
}
}

我收到此错误:Cannot resolve method 'onActivityResult(int, int, android.content.Intent)' 有什么办法可以避免这种情况吗?或者是否可以在适配器类中使用此方法?如果不是我该怎么办?



1> kandroidj..:

你不能在任何Adapter类中拥有它,这只是一个内部方法Activity,每个类extends Activity都能够覆盖这个方法,但肯定不是一个适配器.

请参阅java中的Overrides

onActivityResult请参阅活动#onActivityResult(INT,INT,意图)

对于类似的东西,RecyclerView.Adapter您将要在适配器中定义接口并在Activity中实现该接口.

例如,此处是应用程序的代码,该应用程序将单击的项目的String及其位置发送到显示它的新Activity:

主要活动:

public class MainActivity extends AppCompatActivity implements MyAdapter.CallbackInterface {


    private static final int MY_REQUEST = 1001;

    private static final String [] ITEMS = {"Item 1", "Item 2", "Item 3" ,"Item 4", "Item 5", "Item 6"};

    private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ... Adapter setup, simple LinearLayoutManager and String [] used
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec_view);
        myAdapter = new MyAdapter(this, ITEMS);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(resultCode){

            case RESULT_OK:

                // ... Check for some data from the intent
                if(requestCode == MY_REQUEST){
                    // .. lets toast again
                    int position = -1;
                    if(data != null){
                        position = data.getIntExtra("Position", 0);
                    }

                    if(position != -1) {
                        Toast.makeText(this, "Handled the result successfully at position " + position, Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(this, "Failed to get data from intent" , Toast.LENGTH_SHORT).show();
                    }
                }

                break;

            case RESULT_CANCELED:

                // ... Handle this situation
                break;
        }
    }

    /**
     * Interface Method which communicates to the Acitivty here from the {@link MyAdapter}
     * @param position - the position
     * @param text - the text to pass back
     */
    @Override
    public void onHandleSelection(int position, String text) {

        Toast.makeText(this, "Selected item in list "+ position + " with text "+ text, Toast.LENGTH_SHORT).show();

        // ... Start a new Activity here and pass the values
        Intent secondActivity = new Intent(MainActivity.this, DetailActivity.class);
        secondActivity.putExtra("Text",text);
        secondActivity.putExtra("Position", position);
        startActivityForResult(secondActivity, MY_REQUEST);
    }
}

MainActivity的xml:




    

MyAdapter:

public class MyAdapter extends RecyclerView.Adapter{

    private CallbackInterface mCallback;
    private String [] mMyItems;
    private Context mContext;

    public interface CallbackInterface{

        /**
         * Callback invoked when clicked
         * @param position - the position
         * @param text - the text to pass back
         */
        void onHandleSelection(int position, String text);
    }

    public MyAdapter(Context context, String [] myItems){

        mContext = context;
        mMyItems = myItems;

        // .. Attach the interface
        try{
            mCallback = (CallbackInterface) context;
        }catch(ClassCastException ex){
            //.. should log the error or throw and exception
            Log.e("MyAdapter","Must implement the CallbackInterface in the Activity", ex);
        }
    }

    @Override
    public int getItemCount() {
        return mMyItems.length;
    }

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

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {

        // Set the text for the View
        holder.mTextView.setText(mMyItems[position]);

        // Use your listener to pass back the data
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCallback != null){
                    mCallback.onHandleSelection(position, mMyItems[position]);
                }
            }
        });
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        return new MyHolder(view);
    }

    static class MyHolder extends RecyclerView.ViewHolder{

        TextView mTextView;

        public MyHolder(View itemView) {
            super(itemView);

            mTextView = (TextView) itemView.findViewById(R.id.tv_list_item);
        }
    }
}

适配器的布局:




    

和DetailActivity:

public class DetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_activity);

        TextView textView = (TextView) findViewById(R.id.tv_detail_text);

        if(getIntent() != null && getIntent().getExtras() != null) {
            Bundle extras = getIntent().getExtras();
            String text = extras.getString("Text");
            int position = extras.getInt("Position");

            textView.setText(String.format("%s at position %d", text, position));

            // .. For this purpose set result okay and use position as part of data when Activity finishes
            // Which will in turn invoke the onActivityResult method in the calling activity
            Intent resultIntent = new Intent();
            resultIntent.putExtra("Position", position);
            setResult(RESULT_OK, resultIntent);
        }
    }
}

详细活动的布局:





    

祝你好运,编码愉快!

推荐阅读
echo7111436
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有