Пожалуйста, посмотрите мой код и снимок того, что я ищу, я создал новое действие, из которого я могу обновить информацию о моем продукте ( примечание: я добавлю функцию загрузки изображений позже в моем действии ).но из моего кода я получаю результат, такой как прикрепленный снимок, где я пометил красным то, что хотел.Я добавляю продукты как администратор и пытаюсь обновить их как администратор, но из другой деятельности я хотел обновить свои данные.Пожалуйста, помогите мне
Вот код активности, которую я пытаюсь
![enter image description here](https://i.stack.imgur.com/YEmGI.jpg)
package com.commerce.daily.dailycommerce;
import android.app.ProgressDialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.commerce.daily.dailycommerce.ViewHolder.ProductViewHolder;
import com.commerce.daily.dailycommerce.model.Products;
import com.commerce.daily.dailycommerce.prevalent.prevalent;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.HashMap;
public class AdminUpdatingProductsActivity extends AppCompatActivity {
private String UpdateDescription, updatePrice, updatePName;
private TextView updateTxtBtn, changeImageTxt, closeTxtBtn;
private EditText productUpdateName, productUpdateDescription, productUpdatePrice;
private DatabaseReference updateProductRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_updating_products);
updateProductRef = FirebaseDatabase.getInstance().getReference().child("Products");
updateTxtBtn = (TextView) findViewById(R.id.update_product_details_btn2);
changeImageTxt = (TextView) findViewById(R.id.product_image_update_txt);
closeTxtBtn = (TextView) findViewById(R.id.close_update_product_btn);
productUpdateName = (EditText) findViewById(R.id.update_product_name);
productUpdateDescription = (EditText) findViewById(R.id.update_product_description);
productUpdatePrice = (EditText) findViewById(R.id.update_product_price);
updateTxtBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
updateinformation();
}
});
}
private void updateinformation() {
updatePName = productUpdateName.getText().toString();
UpdateDescription = productUpdateDescription.getText().toString();
updatePrice = productUpdatePrice.getText().toString();
HashMap<String, Object> ProductMap = new HashMap<>();
ProductMap.put("pname", updatePName);
ProductMap.put("price", updatePrice);
ProductMap.put("description", UpdateDescription);
updateProductRef.updateChildren(ProductMap);
}
}
А вот и xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.commerce.daily.dailycommerce.AdminUpdatingProductsActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_update_product"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimary">
<android.support.v7.widget.Toolbar
android:id="@+id/tollbar_update_product"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/close_update_product_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:textColor="@android:color/white"
android:textSize="17sp"
android:textStyle="bold"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/update_product_details_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:textColor="@android:color/white"
android:textSize="17sp"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ImageView
android:id="@+id/product_image_update"
android:layout_below="@+id/app_bar_update_product"
android:layout_width="250dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:src="@drawable/ic_menu_camera"/>
<TextView
android:id="@+id/product_image_update_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/product_image_update"
android:text="Change Image"
android:layout_marginTop="4dp"
android:textColor="@android:color/black"
android:textSize="17sp"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/update_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/product_image_update_txt"
android:inputType="textMultiLine"
android:layout_marginTop="30dp"
android:padding="20dp"
android:hint="Product Name..."
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:background="@drawable/input_design"/>
<EditText
android:id="@+id/update_product_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/update_product_name"
android:inputType="textMultiLine"
android:layout_marginTop="6dp"
android:padding="20dp"
android:hint="Product Description..."
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:background="@drawable/input_design"/>
<EditText
android:id="@+id/update_product_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/update_product_description"
android:inputType="textMultiLine"
android:layout_marginTop="6dp"
android:padding="20dp"
android:hint="Product Price..."
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:background="@drawable/input_design"/>
</RelativeLayout>
package com.commerce.daily.dailycommerce;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import com.commerce.daily.dailycommerce.Interface.ItemClickListner;
import com.commerce.daily.dailycommerce.ViewHolder.ProductViewHolder;
import com.commerce.daily.dailycommerce.model.Products;
import com.commerce.daily.dailycommerce.prevalent.prevalent;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.squareup.picasso.Picasso;
import java.util.HashMap;
public class AdminListedProductActivity extends AppCompatActivity
{
private DatabaseReference AdminProductsRef;
private RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_listed_product);
AdminProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");
recyclerView = findViewById(R.id.productlist_recycler);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Products> options =
new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(AdminProductsRef, Products.class)
.build();
final FirebaseRecyclerAdapter<Products, ProductViewHolder> adapter =
new FirebaseRecyclerAdapter<Products, ProductViewHolder>(options)
{
@Override
protected void onBindViewHolder(@NonNull ProductViewHolder holder, final int position, @NonNull final Products model)
{
holder.txtProductName.setText(model.getPname());
holder.txtProductDescription.setText(model.getDescription());
holder.txtProductPrice.setText("Price = "+ model.getPrice());
Picasso.get().load(model.getImage()).into(holder.imageView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
CharSequence options[] = new CharSequence[]
{
"Edit",
"Remove"
};
AlertDialog.Builder builder = new AlertDialog.Builder(AdminListedProductActivity.this);
builder.setTitle("Edit/Delete");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
if(which == 0)
{
updateProducts();
}
if(which == 1)
{
FirebaseDatabase.getInstance().getReference().child("Products")
.child(model.getDate() + model.getTime())
.removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful())
{
Toast.makeText(AdminListedProductActivity.this, "Item Removed Successfully.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AdminListedProductActivity.this, AdminCategoryActivity.class);
startActivity(intent);
}
}
});
}
}
});
builder.show();
}
});
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items_layout, parent, false);
ProductViewHolder holder = new ProductViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
private void updateProducts()
{
finish();
}
@Override
public void onBackPressed()
{
super.onBackPressed();
}
}
и вот мой класс продуктов в пакете под названием модель
package com.commerce.daily.dailycommerce.model;
/**
* Created by Romen on 2/10/2019.
*/
public class Products
{
private String pname, description, price,image, category, pid, date, time;
public Products()
{
}
public Products(String pname, String description, String price, String image, String category, String pid, String date, String time)
{
this.pname = pname;
this.description = description;
this.price = price;
this.image = image;
this.category = category;
this.pid = pid;
this.date = date;
this.time = time;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
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 getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}