设置意图,在回收者视图中选择OnCreate转到特定列表(行)的另一个特定活动

时间:2019-06-20 13:42:02

标签: java android android-intent android-recyclerview onclick

我已经实现了<template> <div class=""> <div v-for="(device,index) in value" :key="index"> <div v-if="device.hasOwnProperty('timeIn')" class="row" > <transition :duration = "700" type="animation" enter-active-class="bounceInLeft" leave-active-class="bounceInRight" appear mode="out-in" key="1"> <app-user-detected :userDetected="device" ></app-user-detected key="1"> </transition> </div> <div v-if="device.hasOwnProperty('timeOut')" class="out row" :key="3"> <transition :duration = "400" type="animation" enter-active-class="fadeIn" leave-active-class="fadeOut" appear mode="out-in"> <app-user-detail :userDetail="device"></app-user-detail> </transition> </div> </div> </div> </template> ,并且当我单击每个列表时,所有操作都将进入“ abc”活动,但是我想将具有单词“ dd”的列表导航到活动“ abcActivity”中,并将具有单词“ ss”的列表导航到转到“ defActivity”。

我尝试添加另外一个onClick,但所有列表再次显示相同的内容:(

请帮助我,我在撰写本文时阅读了所有建议的文章,但没有一篇是我所要求的答案。请帮助我。

这是主要活动:

intent

这是我在“ abc”的oncreate下添加的代码

public class MainActivity extends AppCompatActivity implements WordAdapter.OnNoteListener {
    private RecyclerView recyclerView;
    private static final String TAG = MainActivity.class.getCanonicalName();

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

        recyclerView = findViewById(R.id.home_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);

        List<Word> wordList = new ArrayList<>();


        wordList.add(new Word(R.drawable.ic_launcher_background, "dd"));
        wordList.add(new Word(R.drawable.ic_launcher_background, "ss"));
        wordList.add(new Word(R.drawable.ic_launcher_background, "cc"));


        WordAdapter adapter = new WordAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.addItems(wordList);
    }

    @Override
    public void onNoteClick(int position) {
        Log.d(TAG, "clicked on the position:" + position);


        Intent myIntent = new Intent(MainActivity.this, abc.class);
        myIntent.putExtra("dd", 1); 
        MainActivity.this.startActivity(myIntent);


        Intent feedbackIntent = new Intent(MainActivity.this, def.class);
        feedbackIntent.putExtra("ss", 0); 
        MainActivity.this.startActivity(feedbackIntent);

    }
}

这是我在“ def”的oncreate下添加的代码

Intent myIntent = getIntent();
String value = myIntent.getStringExtra("dd" );

这是名为WordAdapter的适配器

Intent intent = getIntent();
        String value = intent.getStringExtra("ss");

我知道我错了:(。我是新手,请容忍这些并推荐解决方案。在此处输入代码

2 个答案:

答案 0 :(得分:0)

尝试一下

在这里,我正在传递recyclerview项的内容,分别是活动的标题。这样我们就可以在MainActivity中进行处理了。

在您的WordAdapter类中,将此下面的方法插入ViewHolder

@Override
public void onClick(View view) {
   onNoteListener.onNoteClick(getAdapterPosition(), wordList.get(position).getTitle());
}

并像下面的代码一样更改您的interface参数,

 public interface OnNoteListener {
        void onNoteClick(int position, String title);
    }

后跟MainActivity,从WordAdapter传递值集Intent到适当的活动,如下面的代码。

@Override
    public void onNoteClick(int position, String title) {
        Log.d(TAG, "clicked on the position:" + position);
        if(title.equalsIgnoreCase("dd"){
            Intent myIntent = new Intent(MainActivity.this, abc.class);
            myIntent.putExtra("dd", 1); 
            MainActivity.this.startActivity(myIntent);
        }
        else {
            Intent feedbackIntent = new Intent(MainActivity.this, def.class);
            feedbackIntent.putExtra("ss", 0); 
            MainActivity.this.startActivity(feedbackIntent);
        }

    }

答案 1 :(得分:0)

我只是尝试使用与自己不同的方法,最终这个方法起作用了,我是初学者,当我确定它时,我非常高兴:p

要从回收商传递意图,则无需在发送意图的地方编写任何代码。

只需通过if else语句在onNoteLitner中编写以下给出的代码:

@Override
    public void onNoteClick(int position) {
        Log.d(TAG, "clicked on the position:" + position);
        if (position == 0) {
            Intent intent = new Intent(NoteActivity.this, RajeshDai.class);
            startActivity(intent);
        }

如果您拥有我的话,那之后将Adapter保持不变,我的意思是如果您有上面提到的我的适配器。

然后,在Word中实现Parcelable(提供图像,文本)到适配器,然后它应该如下所示:

public class Word implements Parcelable {

    private int imageResource;
    private String title;

    public Word(int imageResource, String title) {
        this.imageResource = imageResource;
        this.title = title;
    }

    protected Word(Parcel in) {
        imageResource = in.readInt();
        title = in.readString();
    }

    public static final Creator<Word> CREATOR = new Creator<Word>() {
        @Override
        public Word createFromParcel(Parcel in) {
            return new Word(in);
        }

        @Override
        public Word[] newArray(int size) {
            return new Word[size];
        }
    };

    public int getImageResource() {
        return imageResource;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(imageResource);
        parcel.writeString(title);
    }
}

随时询问您是否遇到问题,我会尽力帮助您!