将当前ItemId从RecyclerAdapter传递到其他类

我想将Adapter的当前ItemID位置传递给另一个类,但是问题是每当我从下一个类切换位置时,​​它总是从零位置开始。 我已经使用getItemId来获取卡的位置,但是问题是每当我使用IngameWrkfunction中的位置来获取数据时,它总是从初始点而不是从当前页面获取数据。

RecyclerAdapterClass

package com.example.apptuzzle;

import android.content.Context;
import android.content.Intent;
import android.icu.text.CaseMap;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mcontext;
    private List<InGameContent> mData;


    public RecyclerViewAdapter(Context mcontext, List<InGameContent> mData){
        this.mcontext = mcontext;
        this.mData = mData;
    }

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

        View view;
        LayoutInflater mInflater = LayoutInflater.from(mcontext);
        view = mInflater.inflate(R.layout.cardview_items1,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
         holder.Title.setText(mData.get(position).getTitle());
         holder.Title.setTag(position);

        holder.cardview.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 int  pos = (int)  getItemId(position) + 1;   // when the int is set to one it opens get the data from 1 position 
                 Intent intent = new Intent(mcontext,InGame_wrkfunction.class);
                 intent.putExtra("pos", pos);
                 //passing data to the InGame_wrkfunction.class
                 intent.putExtra("MyData", (Parcelable) mData.get(position));
                 // start the activity
                 mcontext.startActivity(intent);


             }


         });


    }

    @Override
    public int getItemCount() {
        return mData.size();
    }


  public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView Title;
        CardView cardview;

        public MyViewHolder(View itemView) {
            super(itemView);
            Title = (TextView) itemView.findViewById(R.id.LvL_id1);
            cardview = (CardView) itemView.findViewById(R.id.cardview_id);

        }



    }
}
在游戏功课中
package com.example.apptuzzle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

public class InGame_wrkfunction extends AppCompatActivity  {
    private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btndot;                //Buttons Declaration
    private Button btnclr, btnsubm, popup_g_button1;
    private TextView txtv1, txtv2, popup_g_msg1, popup_g_des1,highscore1;
    int scores = 0;


    private LinearLayout Popup_layout11;
    private Animation pop_animation;
    int pos ;

    private ImageView img,popup_g_img1,popup_b_img;                                                      //Question Declaration
    private TextView txt;

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


        btn0 = (Button) findViewById(R.id.Btn0);
        btn1 = (Button) findViewById(R.id.Btn1);
        btn2 = (Button) findViewById(R.id.Btn2);
        btn3 = (Button) findViewById(R.id.Btn3);
        btn4 = (Button) findViewById(R.id.Btn4);
        btn5 = (Button) findViewById(R.id.Btn5);
        btn6 = (Button) findViewById(R.id.Btn6);
        btn7 = (Button) findViewById(R.id.Btn7);
        btn8 = (Button) findViewById(R.id.Btn8);
        btn9 = (Button) findViewById(R.id.Btn9);
        btndot = (Button) findViewById(R.id.Btndot);
        btnclr = (Button) findViewById(R.id.BtnClr);
        btnsubm = (Button) findViewById(R.id.BtnSubm);
        img = (ImageView) findViewById(R.id.gamethumbnail);
        txtv1 = (TextView) findViewById(R.id.TxtV1);
        txt = (TextView) findViewById(R.id.TxtV2);
        Popup_layout11 = findViewById(R.id.Popup_layout11);
        txt = (TextView) findViewById(R.id.TxtV2);
        popup_g_button1 = findViewById(R.id.Popup_G_button1);
        popup_g_msg1 = findViewById(R.id.Popup_G_msg1);
        popup_g_des1 = findViewById(R.id.Popup_G_des1);
        popup_g_img1 = findViewById(R.id.Popup_G_Img12);
        highscore1 = findViewById(R.id.ScoreCounter1);



        pop_animation = AnimationUtils.loadAnimation(this, R.anim.pop_up);

        btn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "0");
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "1");
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "2");
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "3");
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "4");
            }
        });
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "5");
            }
        });
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "6");
            }
        });
        btn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "7");
            }
        });
        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "8");
            }
        });
        btn9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "9");
            }
        });
        btndot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + ".");
            }
        });
        btnclr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackground(null);
                txtv1.setText(null);
            }
        });


        // Receive data

        Intent intent = getIntent();
        final InGameContent inGameContent = (InGameContent) intent.getParcelableExtra("MyData");
        pos = intent.getExtras().getInt("pos");


        setData(inGameContent);


        //Load Scores
        SharedPreferences myscores = this.getSharedPreferences("MyAwesomeScores", Context.MODE_PRIVATE);
        scores = myscores.getInt("scores", 0);  //0 tha phehele
        highscore1.setText("Scores:" + scores);


        btnsubm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String a = txtv1.getText().toString();
                if (a.equals(inGameContent.getAnswrr())) {
                    scores += 30;
                    //Save scores
                    SharedPreferences myscores = getSharedPreferences("MyAwesomeScores", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = myscores.edit();
                    editor.putInt("scores", scores);
                    editor.commit();
                    v.setOnClickListener(null);//Remove setOnClickListener

                    highscore1.setText("Scores:" + scores);


                    Popup_layout11.setVisibility(LinearLayout.VISIBLE);
                    Popup_layout11.setAnimation(pop_animation);
                    Popup_layout11.animate();
                    pop_animation.start();
                } else {
                    scores -= 30;

                    //Save scores
                    SharedPreferences myscores = getSharedPreferences("MyAwesomeScores", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = myscores.edit();
                    editor.putInt("scores", scores);
                    editor.commit();
                    v.setOnClickListener(null);//Remove setOnClickListener

                    highscore1.setText("Scores:" + scores);


                    popup_g_img1.setImageResource(R.drawable.sadface11);
                    popup_g_msg1.setText("Ohh Snap!");
                    popup_g_des1.setText("Don't worry Kid you just tap the button to move on to the next lesson. REMEMBER 'never waste time'.");
                    Popup_layout11.setVisibility(LinearLayout.VISIBLE);
                    Popup_layout11.setAnimation(pop_animation);
                    Popup_layout11.animate();
                    pop_animation.start();
                }
            }
        });

        popup_g_button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Play_Games1 play_games1 = new Play_Games1();
                setData(play_games1.getLstBook().get(pos));
                pos += 1;

            }
        });


    }
    private void setData(InGameContent inGameContent){
        //Setting Values
        img.setImageResource(inGameContent.getThumbnail());                                                         ////It ll replace the image that was fetched previously
        txt.setText(inGameContent.getDescription());

    }

}





/*Two Ways of Putting One Animation In an activity
* 1st One
*        Anim1 = AnimationUtils.loadAnimation(this, R.anim.new_animfile1);

      //  pop_img1.setVisibility(View.GONE);
        Btn1.setOnClickListener(new View.OnClickListener() {                <-----In this type you can put animation inside On Click Listener
            @Override
            public void onClick(View v) {

                Layout1.setVisibility(LinearLayout.VISIBLE);
                Anim1.setDuration(500);
                Layout1.setAnimation(Anim1);
                Layout1.animate();
                Anim1.start();

            }
        });
*
*
*  2nd One
* public class MyActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);                                            <-----In this type you can set another class outside the OnCreate Bundle
 }

public void animate(View view){
    LinearLayout dialog   = (LinearLayout)findViewById(R.id.dialog);
    dialog.setVisibility(LinearLayout.VISIBLE);
    Animation animation   =    AnimationUtils.loadAnimation(this, R.anim.anim);
    animation.setDuration(500);
    dialog.setAnimation(animation);
    dialog.animate();
    animation.start();
 }

}
* */
评论
Cooke
Cooke
  holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos= myData.get(position);
            Intent intent=new Intent(mcontext,InGame_wrkfunction.class);
            intent.putExtra("myData", pos);
            mcontext.startActivity(intent);
        }
    });
点赞
评论