将值从一个Firebase Recycler Adapter传递到另一个Activity

时间:2018-02-17 11:59:52

标签: android firebase

我想将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

1 个答案:

答案 0 :(得分:0)

以及我已经看到你的代码最好将你的适配器放在一个单独的类中,但是在你的适配器实现中你应该调用on click函数。

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");

希望这会有所帮助