如何在回收者视图中更新多个sqlite数据?

时间:2018-11-20 06:12:46

标签: android sqlite sql-update

我有一个商人清单CARDVIEW。当我单击CARDVIEW中的UPDATE按钮时,它将获取该特定商人的所有数据并转到另一个屏幕,该屏幕也是CARDVIEW。然后,我想更新所有数据,然后按一个更新按钮以更新所有行。我该怎么做呢?我正在使用RecyclerVie和adapter.This是第二个屏幕。每个字段都是编辑文本。我想编辑并单击更新分配按钮,以便所有数据都在sqlite中更新

这是我的java类     包com.example.user.paperflyv0;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.ContactsContract;
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.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class UpdateAssigns extends AppCompatActivity {

    RecyclerView recyclerView_ua;
    RecyclerView.LayoutManager layoutManager_ua;
    RecyclerView.Adapter adapter_ua;
    List<UpdateAssign_Model> updateAssignModelList;
    Database database;
    Button button;
    String exu_name,counts;
    Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        database=new Database(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_assigns);
        updateAssignModelList = new ArrayList<>();
        TextView mName = findViewById(R.id.merchant_name_update);
        mName.setText(getIntent().getStringExtra("MERCHANTNAME"));
        final String merchantcode = getIntent().getStringExtra("MERCHANTCODE");


        recyclerView_ua = (RecyclerView) findViewById(R.id.recycler_view_ua);
        recyclerView_ua.setHasFixedSize(true);
        layoutManager_ua = new LinearLayoutManager(this);
        recyclerView_ua.setLayoutManager(layoutManager_ua);
        getData(merchantcode);



    }

    private void getData(final String merchantcode)
    {
        try{

            SQLiteDatabase sqLiteDatabase = database.getReadableDatabase();
            Cursor c = database.getassignedexecutive(sqLiteDatabase,merchantcode);
            while (c.moveToNext())
            {
                String name = c.getString(0);
                String count = c.getString(1);
                UpdateAssign_Model updateAssign_model = new UpdateAssign_Model(name,count);
                updateAssignModelList.add(updateAssign_model);
            }
            adapter_ua = new UpdateAssignsAdapter(updateAssignModelList,getApplicationContext());
            recyclerView_ua.setAdapter(adapter_ua);

            }catch (Exception e)
        {
            Toast.makeText(getApplicationContext(), "some error" ,Toast.LENGTH_SHORT).show();
        }
    }

}

这是我的适配器-

package com.example.user.paperflyv0;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class UpdateAssignsAdapter extends RecyclerView.Adapter<UpdateAssignsAdapter.ViewHolder> {

   private List<UpdateAssign_Model> updateAssignModelList;
   private Context context;
   Button button;





    public UpdateAssignsAdapter(List<UpdateAssign_Model> updateAssignModelList,Context context) {
        this.updateAssignModelList = updateAssignModelList;
        this.context=context;

    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public AutoCompleteTextView itemExe;
        public EditText itemCount;


        public ViewHolder(View itemView) {
            super(itemView);
            itemExe = (AutoCompleteTextView)itemView.findViewById(R.id.auto_complete);
            itemCount = (EditText)itemView.findViewById(R.id.order_count);


        }
    }

    @Override
    public UpdateAssignsAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.update_assigns_layout, viewGroup, false);
        UpdateAssignsAdapter.ViewHolder viewHolder = new UpdateAssignsAdapter.ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(UpdateAssignsAdapter.ViewHolder viewHolder, int i) {
        UpdateAssign_Model updateAssign_model = updateAssignModelList.get(i);
        viewHolder.itemExe.setText(updateAssign_model.getEx_name());
        viewHolder.itemCount.setText(updateAssign_model.getCount());
        /*viewHolder.itemCount.setTag(R.id.order_count,i);*/
        }

    @Override
    public int getItemCount() {
        return updateAssignModelList.size();
    }

}

1 个答案:

答案 0 :(得分:0)

    viewHolder.itemCount.addTextChangedListener(new TextWatcher() {

       public void afterTextChanged(Editable s) {}

       public void beforeTextChanged(CharSequence s, int start,
         int count, int after) {
       }

       public void onTextChanged(CharSequence s, int start,
         int before, int count) {
// IF YOU USE HANDLER HERE IT WILL BE MORE EFFICIENT
         updateAssign_model.setCount();
       }
      });

将此代码放在适配器中