簡體   English   中英

將值從一個 Firebase Recycler Adapter 傳遞給另一個 Activity

[英]Pass value from one Firebase Recycler Adapter to another Activity

我想將 product_name、product_price 和數量的值從 firebase 適配器傳遞給 Product_viewer Activity。 我怎么能找到用戶點擊回收器視圖的哪個部分。

我的主要活動是 User_home_page。

Dataprovider.java


package com.example.alok.shoppers1.User_data.RecyclerView;

public class Dataprovider {
private String category;
private String email_id;
private String product_id;
private String product_name;
private String product_price;
private String quantity;

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getProduct_name() {
    return product_name;
}

public void setProduct_name(String product_name) {
    this.product_name = product_name;
}

public String getEmail_id() {
    return email_id;
}

public void setEmail_id(String email_id) {
    this.email_id = email_id;
}

public String getProduct_id() {
    return product_id;
}

public void setProduct_id(String product_id) {
    this.product_id = product_id;
}

public String getProduct_price() {
    return product_price;
}

public void setProduct_price(String product_price) {
    this.product_price = product_price;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public Dataprovider(String category, String product_name, String email_id, String product_id, String product_price, String quantity) {
    this.category = category;
    this.product_name = product_name;
    this.email_id = email_id;
    this.product_id = product_id;
    this.product_price = product_price;
    this.quantity = quantity;

}

public Dataprovider() {

}
}

這是我的 User_home_page User_home_page

package com.example.alok.shoppers1.User_data;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.alok.shoppers1.R;
import com.example.alok.shoppers1.User_data.RecyclerView.Dataprovider;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;


public class User_home_page extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private RecyclerView mDataprovider;
private DatabaseReference mDatabase;


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

    mDatabase = FirebaseDatabase.getInstance().getReference().child("product");
    mDatabase.keepSynced(true);

    mDataprovider = findViewById(R.id.recycler_view);
    mDataprovider.setHasFixedSize(true);
    mDataprovider.setLayoutManager(new LinearLayoutManager(this));


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<Dataprovider,DataproviderViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Dataprovider, DataproviderViewHolder>(Dataprovider.class,R.layout.user_item_layout,DataproviderViewHolder.class,mDatabase) {
        @Override
        protected void populateViewHolder(DataproviderViewHolder viewHolder, Dataprovider model, int position) {

            viewHolder.setProduct_price(model.getProduct_price());
            viewHolder.setQuantity(model.getQuantity());
            viewHolder.setProduct_name(model.getProduct_name());
        }
    };

    mDataprovider.setAdapter(firebaseRecyclerAdapter);
}

public static class DataproviderViewHolder extends RecyclerView.ViewHolder{
    View mview;
    public DataproviderViewHolder(View itemView){
        super(itemView);

        mview = itemView;
    }
    public void setProduct_price(String price){
        TextView prod_price = mview.findViewById(R.id.price_product_user);
        prod_price.setText(price);
    }
    public void setQuantity(String quantity){
        TextView quant = mview.findViewById(R.id.product_quantity_user);
        quant.setText(quantity);
    }
    public void setProduct_name(String name){
        TextView nam = mview.findViewById(R.id.product_name_user);
        nam.setText(name);
    }
}





@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.user_home_page, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.electronics) {
        // Handle the camera action
    } else if (id == R.id.user_profile) {
        Intent user_profile = new  Intent(getBaseContext(),User_profile.class);
        startActivity(user_profile);

    } else if (id == R.id.lifestyle) {

    } else if (id == R.id.home_appliances) {

    } else if (id == R.id.books_and_more) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}


}

我想將數據從 user_home_page 傳遞給 data_viewer Intent

正如我已經看到你的代碼最好把你的適配器放在一個單獨的類中但是在你的適配器實現中你應該調用點擊函數。

protected void onStart() {
super.onStart();

FirebaseRecyclerAdapter<Dataprovider,DataproviderViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Dataprovider, DataproviderViewHolder>(Dataprovider.class,R.layout.user_item_layout,DataproviderViewHolder.class,mDatabase) {
    @Override
    protected void populateViewHolder(DataproviderViewHolder viewHolder, Dataprovider model, int position) {

        viewHolder.setProduct_price(model.getProduct_price());
        viewHolder.setQuantity(model.getQuantity());
        viewHolder.setProduct_name(model.getProduct_name());
        //set your click listener here
        viewholder.setOnClickListener(new View.onClickListener(){...
         //to pass data you could either put them seperatly in new intent
        Intent i = new Intent(CurrentActivity, NextActivity.class);
        i.putExtra("quantity", model.getQuantity());
        // all data needed then
        startActivity(i);

        //or you could make your object model extend Serializable and send the object as it is to the next activity as
       i.putExtra("model", model);
     });
    }
};

mDataprovider.setAdapter(firebaseRecyclerAdapter);
}

public static class DataproviderViewHolder extends 
RecyclerView.ViewHolder{
View mview;
public DataproviderViewHolder(View itemView){
    super(itemView);

    mview = itemView;
}
public void setProduct_price(String price){
    TextView prod_price = mview.findViewById(R.id.price_product_user);
    prod_price.setText(price);
}
public void setQuantity(String quantity){
    TextView quant = mview.findViewById(R.id.product_quantity_user);
    quant.setText(quantity);
}
public void setProduct_name(String name){
    TextView nam = mview.findViewById(R.id.product_name_user);
    nam.setText(name);
}
}

在您的下一個活動中,您可以按如下方式獲取數據

//First method 
String qty = getIntent().getStringExtra("quantity");

或者如果您選擇發送可序列化的對象

//Second method
Model model = getIntent().getSerializableExtra("model");

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM