我有recyclerview,其中显示的项目名称分别为+/-,由+/-按钮增加和减少的项目名称,项目速率和数量。现在,我想从recyclerview的每个项目中获取所有值,并将其通过服务器发送或将其保存到本地数据库,以便实现该目标。
//这是我的适配器类
公共类TeaListAdapter扩展了RecyclerView.Adapter {
//private int num=0;
private List<TeaListPOJO> teaItemList;
private Context mContext;
private Cursor cursor;
int comboCount;
HashMap<Object,Integer> selectedMap = new HashMap ();
private String ItemName;
private String itemrate;
private String qty;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvitemName, tvitemRate,tvcount; //number
public ImageView ivItemImg,ivPlus,ivMinus;
public Button btnIncrease,btnDecrease;
RecyclerView.ViewHolder holder;
public MyViewHolder(View view) {
super(view);
tvitemName = (TextView) view.findViewById(R.id.txt_item_name);
tvitemRate = (TextView) view.findViewById(R.id.txt_item_price);
ivItemImg= (ImageView) view.findViewById (R.id.iv_item);
ivPlus=(ImageView) view.findViewById (R.id.row_view_final_order_iv_plus);
ivMinus=(ImageView) view.findViewById (R.id.row_view_final_order_iv_minus);
tvcount=(TextView) view.findViewById (R.id.row_view_final_order_tv_count);
}
}
public TeaListAdapter(List<TeaListPOJO> teaItemList) {
this.mContext=mContext;
this.cursor=cursor;
this.teaItemList = teaItemList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rv_placeorder_items, parent, false);
return new MyViewHolder (itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
TeaListPOJO tealist = teaItemList.get(position);
holder.tvitemName.setText(tealist.getItemName ());
holder.tvitemRate.setText(AppConstants.INDIAN_RUPEE_SIGN.concat (tealist.getItemRate ()));
holder.ivPlus.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
int count=0;
try{
count = Integer.parseInt(holder.tvcount.getText().toString());
}
catch(Exception e) {
count = 0;
}
//count++;
count = Integer.parseInt(holder.tvcount.getText().toString());
holder.tvcount.setText(String.valueOf(count+ 1));
}
});
holder.ivMinus.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
int count=0;
try{
count = Integer.parseInt(holder.tvcount.getText().toString());
}
catch(Exception e) {
count = 0;
}
if(count>0) {
//count--;
count = Integer.parseInt (holder.tvcount.getText ().toString ());
holder.tvcount.setText (String.valueOf (count - 1));
}
}
});
holder.itemView.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
}
});
/* Intent intent = new Intent (mContext, PlaceOrderActivity.class);
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
intent.putExtra ("quantity", qty);
intent.putExtra ("item", ItemName);
intent.putExtra ("itemrate", itemrate);
ItemName = holder.tvitemName.getText().toString();
qty = holder.tvcount.getText().toString();
itemrate=holder.tvitemRate.getText ().toString ();
Log.e("rate",itemrate);
Log.e("qty",qty);*/
byte[] decodedString = new byte[0];
try {
decodedString = Base64.decode(tealist.getImageStr(), Base64.DEFAULT);
// tenantModelPOJO.setLogo(decodedString);
Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.ivItemImg.setImageBitmap(Bitmap.createScaledBitmap(bmp, 50, 50,false));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return teaItemList.size();
}
}
//这是我的活动班
公共类PlaceOrderActivity扩展了AppCompatActivity,实现了AppConstants,View.OnClickListener,WLAPIcalls.OnAPICallCompleteListener {
private List<TeaListPOJO> teaList = new ArrayList<> ();
private RecyclerView recyclerView;
private TeaListAdapter mAdapter;
private View view;
private Button btnPlaceorder;
EditText edtmsg;
public String str;
private Context mContext = PlaceOrderActivity.this;
private int itemCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_place_order);
str=getIntent ().getStringExtra ("quantity");
edtmsg= (EditText) view.findViewById (R.id.et_message);
edtmsg.setText (str);
setRecyclerView (view);
getallTeaItems ();
}
List<TeaListPOJO> getTeaItemList(String str) {
Gson gson = new Gson ();
Type listType = new TypeToken<List<TeaListPOJO>> () {
}.getType ();
List<TeaListPOJO> myModelList = gson.fromJson (str, listType);
return myModelList;
}
private List<TeaListPOJO> getallTeaItems() {
if (new AppCommonMethods (mContext).isNetworkAvailable ()) {
WLAPIcalls mAPIcall = new WLAPIcalls (mContext, getString (R.string.getTeaItem), this);
mAPIcall.GetTeaItemList ();
} else {
Toast.makeText (mContext, R.string.no_internet, Toast.LENGTH_SHORT).show ();
}
return null;
}
void setRecyclerView(View view) {
recyclerView = (RecyclerView) findViewById (R.id.recycler_view);
mAdapter = new TeaListAdapter (teaList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager (getApplicationContext ());
recyclerView.setLayoutManager (mLayoutManager);
recyclerView.setItemAnimator (new DefaultItemAnimator ());
recyclerView.setAdapter (mAdapter);
}
@Override
public void onClick(View view) {
}
@Override
public void onAPICallCompleteListner(Object item, String flag, String result) throws JSONException {
if (getString (R.string.getTeaItem).equals (flag)) {
Log.e ("Result", result);
teaList = getTeaItemList (result);
setRecyclerView (view);
}
}
}
答案 0 :(得分:0)
TeaListPOJO
还需要一个字段来存储count
。当用户单击+或-按钮并在文本视图中显示count
值时,递增或递减count
值,而不是直接显示。
class TeaListPojo{
private int count;
.....
public int getCount()
return this.count
}
public void setCount(int count)
{
this.count = count;
}
}
这是用于更新计数textview的代码
public void onBindViewHolder(final MyViewHolder holder, final int position) {
TeaListPOJO tealist = teaItemList.get(position);
.....
.....
holder.ivPlus.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
tealist.setCount( tealist.getCount() + 1);
holder.tvcount.setText(String.valueOf(count));
}
});
holder.ivMinus.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
tealist.setCount(tealist.getCount() - 1)
holder.tvcount.setText (String.valueOf (count));
}
}
});
}
答案 1 :(得分:0)
在TeaListPOJO
模型中添加一个额外的属性,比如说count
,当您按+/-按钮时,您必须在模型中设置计数,就像在文本视图中设置计数一样。因此,在适配器中,您具有TeaListPOJO
列表。显示何时要将数据发送到服务器TeaListPOJO
的获取列表,并且还有计数列表。
答案 2 :(得分:0)
在适配器中,设置对API的调用以更改DB中的值。 您不需要传递全部数据,通常您可以通过在URL中传递ID来更新正在查看的一个对象。 例如 https://hostname.domain/api/object/ID 使用PUT请求并传递该行中对象的信息。
如果您也要创建后端,那么我强烈建议您进行设置,因为传递所有行的整个数据集并不是一个理想的解决方案。
如果这些都不是首先存在的,而是全部通过在应用程序中创建而创建的,则在您的主要活动上有一个按钮来保存列表,然后将整个列表传递给API调用,很可能需要将数据解析为JSON。 我假设在这种情况下,如果您是在一个类中创建了这些对象的话。
答案 3 :(得分:0)
,当您增加或减少值时,请始终调用正确的项目设置方法并在模型类中设置值,并且要在服务器上发送该值,然后从modelClass列表中查找值并发送到服务器。