如何解决从适配器到主要活动获取价值
我试图获取使用回收者视图在编辑文本中输入的值。单击提交后,我想将editetxt中给出的文本保存到表中。
-
使用Json将数据存储到表中-成功
-
上一个阵列适配器我得到了数据
-
现在如何在单个提交按钮上将数据从适配器移动到主要活动,以将数据保存到表中。 [在此处输入图片描述] [1]
活动
public class fertlizerRequest extends AppCompatActivity { Button submit_btn,view_btn; RecyclerView material_lv; ArrayList<ProductData> arrayList; ProductsAdapter myAdapterP; private RecyclerView.LayoutManager mLayoutManager; DBHelperClass dbHelperClass = new DBHelperClass(this); Integer[] enterednumber = new Integer[1000]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fertlizer_request); datacasting(); view_btn.setonClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PlantLocationAssy plantLocationAssy = new PlantLocationAssy(); plantLocationAssy.execute(); } }); submit_btn.setonClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(fertlizerRequest.this,"Edited Item are ",Toast.LENGTH_SHORT).show(); } }); } void datacasting(){ material_lv = (RecyclerView) findViewById(R.id.materialreq_lv); submit_btn = (Button) findViewById(R.id.sbt_btn); view_btn = (Button) findViewById(R.id.vie_btn); mLayoutManager = new linearlayoutmanager(this); material_lv.setLayoutManager(mLayoutManager); } void fetchProductData(){ arrayList = dbHelperClass.getProductsDataFromDB(); myAdapterP = new ProductsAdapter(fertlizerRequest.this,arrayList,new ProductsAdapter.onEditTextChanged() { @Override public void onTextChanged(int position,String charSeq) { enterednumber[position] = Integer.valueOf(charSeq); System.out.println("==================================== " + position); Toast.makeText(fertlizerRequest.this,"Entered value is " + myAdapterP.getItemId(position),Toast.LENGTH_SHORT).show(); } }); material_lv.setAdapter(myAdapterP); myAdapterP.notifyDataSetChanged(); System.out.println("==================================== " + myAdapterP.getItemCount() ); } private class PlantLocationAssy extends AsyncTask<Void,Void,Void> { @Override protected Void doInBackground(Void... voids) { return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); fetchProductData(); } @Override protected void onPreExecute() { super.onPreExecute(); } }
}
适配器
package com.example.fertlizertrackerapp.Adapter;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.fertlizertrackerapp.Model.ProductData;
import com.example.fertlizertrackerapp.R;
import java.io.Serializable;
import java.util.ArrayList;
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.Holder> {
private Context context;
private ArrayList<ProductData> arrayList;
private onEditTextChanged onEditTextChanged;
public ProductsAdapter(Context context,ArrayList<ProductData> arrayList,onEditTextChanged onEditTextChanged) {
this.context = context;
this.arrayList = arrayList;
this.onEditTextChanged = onEditTextChanged;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.cust_lv_fertilizer_request,parent,false);
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull final Holder holder,final int position) {
final ProductData productData = arrayList.get(position);
// get for view
String productId = productData.getProductId();
final String material = productData.getMaterial();
String materialCode = productData.getMaterialCode();
//set view
holder.productId.setText(productId);
holder.material.setText(material);
holder.materialCode.setText(materialCode);
holder.quantity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s,int start,int count,int after) {
}
@Override
public void onTextChanged(CharSequence s,int before,int count) {
onEditTextChanged.onTextChanged(position,s.toString());
String qty = holder.quantity.getText().toString();
Toast.makeText(context,"Value Changed is " + material + " : " + qty,Toast.LENGTH_SHORT).show();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public int getItemCount() {
return arrayList.size();
}
class Holder extends RecyclerView.ViewHolder{
TextView productId,material,materialCode;
EditText quantity;
public Holder(@NonNull View itemView) {
super(itemView);
productId = itemView.findViewById(R.id.productId_tv);
material = itemView.findViewById(R.id.materialName_tv);
materialCode = itemView.findViewById(R.id.materialCode_tv);
quantity = (itemView).findViewById(R.id.materialQuantity_edt);
}
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
public interface onEditTextChanged{
void onTextChanged(int position,String charSeq);
}
}
Model
package com.example.fertlizertrackerapp.Model;
public class ProductData {
String ProductId,Material,MaterialCode;
int quantity = 0;
public ProductData(String productId,String material,String materialCode) {
this.ProductId = productId;
this.Material = material;
this.MaterialCode = materialCode;
}
public String getProductId() {
return ProductId;
}
public void setProductId(String productId) {
ProductId = productId;
}
public String getMaterial() {
return Material;
}
public void setMaterial(String material) {
Material = material;
}
public String getMaterialCode() {
return MaterialCode;
}
public void setMaterialCode(String materialCode) {
MaterialCode = materialCode;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
DBHelperClass
package com.example.fertlizertrackerapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import com.example.fertlizertrackerapp.Model.PlantData;
import com.example.fertlizertrackerapp.Model.ProductData;
import com.example.fertlizertrackerapp.Model.ProductRateData;
import java.util.ArrayList;
public class DBHelperClass extends sqliteOpenHelper {
public static final int DBVersion = 1;
public static final String DB_Godown = "FertlizerApp.db";
public static final String DB_ProductListTABLE = "ProductDetails";
public static final String DB_ProductId = "ProductId";
public static final String DB_productStatus = "productStatus";
public static final String DB_Material = "Material";
public static final String DB_MaterialCode = "MaterialCode";
public static final String DB_MaterialStatus = "Status";
public static final String DB_MaterialCreatedBy = "CreatedBy";
public static final String DB_MaterialCreatedOn = "CreatedOn";
public static final String DB_MaterialUpdatedBy = "UpdatedBy";
public static final String DB_MaterialUpdatedOn = "UpdatedOn";
@Override
public void onCreate(sqliteDatabase db) {
db.execsql(" CREATE TABLE " + DB_ProductListTABLE + "(" + DB_ProductId + " TEXT,"
+ DB_productStatus + " TEXT,"
+ DB_Material + " TEXT,"
+ DB_MaterialCode + " TEXT NOT NULL PRIMARY KEY,"
+ DB_MaterialStatus + " TEXT,"
+ DB_MaterialCreatedBy + " TEXT,"
+ DB_MaterialCreatedOn + " TEXT,"
+ DB_MaterialUpdatedBy + " TEXT,"
+ DB_MaterialUpdatedOn + " TEXT)");
}
@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
db.execsql("DROP TABLE IF EXISTS " + DB_ProductListTABLE);
}
public Boolean insertProductDetails(String v_ProductId,String v_productStatus,String v_Material,String v_MaterialCode,String v_MaterialStatus,String v_MaterialCreatedBy,String v_MaterialCreatedOn,String v_MaterialUpdatedBy,String v_MaterialUpdatedOn) {
sqliteDatabase db = this.getWritableDatabase();
ContentValues contentValue = new ContentValues();
contentValue.put("ProductId",v_ProductId);
contentValue.put("productStatus",v_productStatus);
contentValue.put("Material",v_Material);
contentValue.put("MaterialCode",v_MaterialCode);
contentValue.put("Status",v_MaterialStatus);
contentValue.put("CreatedBy",v_MaterialCreatedBy);
contentValue.put("CreatedOn",v_MaterialCreatedOn);
contentValue.put("UpdatedBy",v_MaterialUpdatedBy);
contentValue.put("UpdatedOn",v_MaterialUpdatedOn);
long result = db.insert(DB_ProductListTABLE,null,contentValue);
if (result == -1)
return false;
else
return true;
}
public DBHelperClass(Context context) {
super(context,DB_Godown,DBVersion);
}
public ArrayList<ProductData> getProductsDataFromDB(){
ArrayList<ProductData> arrayList = new ArrayList<>();
sqliteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + DB_ProductListTABLE,null);
while (cursor.movetoNext()) {
String productId = cursor.getString(0);
String material = cursor.getString(2);
String materialCode = cursor.getString(3);
ProductData productData = new ProductData(productId,materialCode);
arrayList.add(productData);
System.out.println("00000000000000000000 Product 00000000000000000000000000000 " + material);
}
return arrayList;
}
}
[1]: https://i.stack.imgur.com/ikNRR.jpg
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。