将信息从一个类传递到另一个类

时间:2021-05-12 11:29:21

标签: java android firebase android-recyclerview

有 2 个类:第一个从 Parsing2 从 Firebase 获取数据,第二个必须继承第一个类的数据。这是将 1 类数据传输到另一个数据库节点、在用户处创建一个新节点并在那里传输信息所必需的。问题是对象很多,它们在rv中作为cv,当你点击cv时,会打开一个新的Activity,有更多关于对象的详细信息,这里也有一个like,当你点击which时, object应该保存为2nd class并添加到当前用户作为我喜欢的游戏列表,我在注册用户时尝试制作收藏夹字段,但该类没有填充,这就是为什么没有变化的原因数据库。 enter image description here
enter image description here

游戏类(第一类的构造函数)

public class Game {
String title;
String tag1;
String tag2;
String tag3;
String tag4;
String price;
String image;
String link;
String video;
String release_date;
String publisher;
String description;
String developer;
String genre;

public Game(){
}

public Game(String title, String tag1, String tag2, String tag3, String tag4,  String price, String image, String link, String video, String release_date, String publisher, String description, String developer, String genre) {
    this.title=title;
    this.tag1=tag1;
    this.tag2=tag2;
    this.tag3=tag3;
    this.tag4=tag4;
    this.price=price;
    this.image=image;
    this.link=link;
    this.video=video;
    this.release_date=release_date;
    this.publisher=publisher;
    this.description=description;
    this.developer=developer;
    this.genre=genre;
}


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getTag1() {
    return tag1;
}

public void setTag1(String tag1) {
    this.tag1 = tag1;
}

public String getTag2() {
    return tag2;
}

public void setTag2(String tag2) {
    this.tag2 = tag2;
}

public String getTag3() {
    return tag3;
}

public void setTag3(String tag3) {
    this.tag3 = tag3;
}

public String getTag4() {
    return tag4;
}

public void setTag4(String tag4) {
    this.tag4 = tag4;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

public String getVideo() {
    return video;
}

public void setVideo(String video) {
    this.video = video;
}

public String getRelease_date() {
    return release_date;
}

public void setRelease_date(String release_date) {
    this.release_date = release_date;
}

public String getPublisher() {
    return publisher;
}

public void setPublisher(String publisher) {
    this.publisher = publisher;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getDeveloper() {
    return developer;
}

public void setDeveloper(String developer) {
    this.developer = developer;
}

public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}

public static final Comparator<Game> BY_TITLE_ASCENDING = new Comparator<Game>() {
    @Override
    public int compare(Game o1, Game o2) {
        return o1.getTitle().compareTo(o2.getTitle());
    }
};

public static final Comparator<Game> BY_TITLE_DESCENDING = new Comparator<Game>() {
    @Override
    public int compare(Game o1, Game o2) {
        return o2.getTitle().compareTo(o1.getTitle());
    }
};}

游戏类(从 Firebase 获取数据)

public class Games extends AppCompatActivity implements View.OnClickListener{

RecyclerView recyclerView;

private DatabaseReference myRef;

private ArrayList<Game> gamesList;
private RecyclerAdapter recyclerAdapter;
private Context mContext;

SharedPreferences preferences;

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    MenuInflater menuInflater =getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);

    MenuItem item=menu.findItem(R.id.action_search);

    androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            recyclerAdapter.getFilter().filter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            recyclerAdapter.getFilter().filter(newText);
            return false;
        }
    });

    return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

    int id= item.getItemId();

    if (id==R.id.action_sort){
        sortDailog();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void sortDailog() {
    String[] options ={"Ascending", "Descending"};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Sort by:");
    builder.setIcon(R.drawable.sort_img);
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            if(which==0){
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("Sort", "ascending");
                editor.apply();
                recreate();
            }

            if(which==1){
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("Sort", "descending");
                editor.apply();
                recreate();
            }
        }
    });

    builder.create().show();
}

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_games);
    ImageButton Home = (ImageButton) findViewById(R.id.home);
    Home.setOnClickListener(this);
    ImageButton Lupa = (ImageButton) findViewById(R.id.lupa);
    Lupa.setOnClickListener(this);
    ImageButton Calendar = (ImageButton) findViewById(R.id.calendar);
    Calendar.setOnClickListener(this);
    ImageButton Kubik = (ImageButton) findViewById(R.id.kubik);
    Kubik.setOnClickListener(this);
    ImageButton Profile = (ImageButton) findViewById(R.id.profile);
    Profile.setOnClickListener(this);

    recyclerView = findViewById(R.id.recyclerview_id);

    preferences=this.getSharedPreferences("My_pref", MODE_PRIVATE);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(new GridLayoutManager(this,2));
    recyclerView.setHasFixedSize(true);


    myRef=FirebaseDatabase.getInstance().getReference();

    gamesList= new ArrayList<>();

    GetDataFromFirebase();

    Toolbar toolbar =findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

}

private void GetDataFromFirebase(){

    Query query_simulator = myRef.child("Parsing2");


    query_simulator.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Game games = new Game();

                games.setImage(snapshot.child("ИЗОБРАЖЕНИЕ").getValue().toString());
                games.setTitle(snapshot.child("НАЗВАНИЕ").getValue().toString());
                games.setTag1(snapshot.child("ТЭГ1").getValue().toString());
                games.setTag2(snapshot.child("ТЭГ2").getValue().toString());
                games.setTag3(snapshot.child("ТЭГ3").getValue().toString());
                games.setTag4(snapshot.child("ТЭГ4").getValue().toString());
                games.setPrice(snapshot.child("ЦЕНА").getValue().toString());
                games.setLink(snapshot.child("ССЫЛКА").getValue().toString());
                games.setVideo(snapshot.child("ВИДЕО").getValue().toString());
                games.setRelease_date(snapshot.child("ДАТА ВЫХОДА").getValue().toString());
                games.setPublisher(snapshot.child("ИЗДАТЕЛЬ").getValue().toString());
                games.setDescription(snapshot.child("ОПИСАНИЕ").getValue().toString());
                games.setDeveloper(snapshot.child("РАЗРАБОТЧИК").getValue().toString());
                games.setGenre(snapshot.child("ЖАНР").getValue().toString());
                gamesList.add(games);


            }

            String mSortSetting = preferences.getString("Sort", "ascending");

            if(mSortSetting.equals("ascending")){
                Collections.sort(gamesList, Game.BY_TITLE_ASCENDING);
            }
            else if(mSortSetting.equals("descending")){
                Collections.sort(gamesList, Game.BY_TITLE_DESCENDING);
            }

            recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
            recyclerView.setAdapter(recyclerAdapter);
            recyclerAdapter.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

private void ClearAll(){
    if(gamesList!=null){
        gamesList.clear();

        if(recyclerAdapter!=null)
            recyclerAdapter.notifyDataSetChanged();
    }

    gamesList= new ArrayList<>();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    ImageView imageView;
    TextView textView, textView1, textView2;


    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        imageView=itemView.findViewById(R.id.game_img_id);
        textView=itemView.findViewById(R.id.game_title_id);
        textView1=itemView.findViewById(R.id.game_tags_id);
        textView2=itemView.findViewById(R.id.game_price_id);
    }

    }

@Override
public void onClick(View v) {
    Intent i;
    switch (v.getId()){
        case R.id.home:
            startActivity(new Intent(this, News.class));
            break;
        case R.id.lupa:
            startActivity(new Intent(this, Games.class));
            break;
        case R.id.calendar:
            startActivity(new Intent(this, Calendar.class));
            break;
        case R.id.kubik:
            startActivity(new Intent(this, Random_game.class));
            break;
        case R.id.profile:
            startActivity(new Intent(this, Profile.class));
            break;


    }
}}

fvdGame类(第二类,需要传递信息的地方,和第一类完全一样)

public class fvdGame {
String title;
String tag1;
String tag2;
String tag3;
String tag4;
String price;
String image;
String link;
String video;
String release_date;
String publisher;
String description;
String developer;
String genre;

public fvdGame() {
}

public fvdGame(String title, String tag1, String tag2, String tag3, String tag4, String price, String image, String link, String video, String release_date, String publisher, String description, String developer, String genre) {
    this.title = title;
    this.tag1 = tag1;
    this.tag2 = tag2;
    this.tag3 = tag3;
    this.tag4 = tag4;
    this.price = price;
    this.image = image;
    this.link = link;
    this.video = video;
    this.release_date = release_date;
    this.publisher = publisher;
    this.description = description;
    this.developer = developer;
    this.genre = genre;
}}

Current_Game类(点击cv打开的当前游戏,按照我的理解,这里需要实现新节点的转移和创建)

public class Current_Game extends AppCompatActivity implements View.OnClickListener{

private TextView textView_title, textView_price, textView_link, textView_publisher,textView_developer,textView_release,textView_description,textView_genre;
private VideoView videoView_promo;
Button button_link;
private FirebaseAuth mAuth;
boolean flag = true;
DatabaseReference myRef;
ArrayList<fvdGame> fvdGamesList;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_current_game);

    ImageButton signInBack = (ImageButton) findViewById(R.id.sign_in_back);
    signInBack.setOnClickListener(this);
    button_link=findViewById(R.id.links_game_id);


    mAuth = FirebaseAuth.getInstance();

    myRef=FirebaseDatabase.getInstance().getReference();

    textView_title=findViewById(R.id.game_title_id);
    textView_price=findViewById(R.id.game_price_id);
    textView_link=findViewById(R.id.links_game_id);
    textView_publisher=findViewById(R.id.publisher_name_id);
    textView_developer=findViewById(R.id.developer_name_id);
    textView_release=findViewById(R.id.release_date_name_id);
    textView_description=findViewById(R.id.game_description_id);
    textView_genre=findViewById(R.id.genre_name_id);
    videoView_promo=findViewById(R.id.banner);

    MediaController mediaController=new MediaController(this);
    videoView_promo.setMediaController(mediaController);
    mediaController.setAnchorView(videoView_promo);

    Intent intent=getIntent();
    String Title=intent.getExtras().getString("Title");
    String Price=intent.getExtras().getString("Price");
    String Link=intent.getExtras().getString("Link");
    String Publisher=intent.getExtras().getString("Publisher");
    String Developer=intent.getExtras().getString("Developer");
    String Release=intent.getExtras().getString("Release");
    String Description=intent.getExtras().getString("Description");
    String Genre=intent.getExtras().getString("Genre");
    String Video=intent.getExtras().getString("Video");


    textView_title.setText(Title);
    textView_price.setText(Price);
    //textView_link.setText(Link);
    textView_publisher.setText(Publisher);
    textView_developer.setText(Developer);
    textView_release.setText(Release);
    textView_description.setText(Description);
    textView_genre.setText(Genre);
    videoView_promo.setVideoPath(Video);

    button_link.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gotoUrl(Link);
        }

        private void gotoUrl(String link) {
            Uri uri=Uri.parse(link);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        }
    });


    final ImageButton imageButton_like = (ImageButton) findViewById(R.id.like_button);
    imageButton_like.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {


                if (flag){
                    imageButton_like.setImageResource(R.drawable.like_form2);
                    Toast.makeText(com.example.adviseriltc.Current_Game.this, "Добавлено в избранное", Toast.LENGTH_SHORT).show();
                    flag = false;

                }else{

                    imageButton_like.setImageResource(R.drawable.like_form1);
                    Toast.makeText(com.example.adviseriltc.Current_Game.this, "Удалено из избранного", Toast.LENGTH_SHORT).show();
                    flag=true;
                }

            }
    });
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.sign_in_back:
        startActivity(new Intent(this, Games.class));
        break;
    }
}}

游戏列表画面和当前游戏画面。
enter image description here enter image description here

0 个答案:

没有答案