使用ID从另一个活动中的SQLite数据库读取数据

时间:2018-12-26 04:07:29

标签: android sqlite

我已经在recyclerview中从Sqlite数据库读取数据

但是当用户单击 next_button 时,它会使用 BANK_ID

从同一行中获取更多数据

在第一个活动中,我已经从recyclerView中的sqlite数据库中读取了一些数据,并且当用户单击next_button时,它使用相同的行从sqlite数据库中获取了更多数据,我已经传递了BANK_ID,它是autoIncrement

这是我的代码...

DataModel类

public class DataModel {

public String BANK_ID;

public String id;
public String farmer_insure_name;
public String farmer_bank_hypo;

public String farmer_name;
public String village;
public String taluka;
public String district;
public String tagging_date;

public String tag_no;
public String animal_species;
public String animal_breed;
public String body_color;
public String shape_right;
public String shape_left;
public String tail_switch;
public String other_marks;
public String prag_status;
public String lactations;
public String milk_qty;
public String sum_insured;

public String tag_photo;
public String head_photo;
public String left_photo;
public String right_photo;
public String farmer_photo;

public String getBANK_ID() {
    return BANK_ID;
}

public void setBANK_ID(String BANK_ID) {
    this.BANK_ID = BANK_ID;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getFarmer_insure_name() {
    return farmer_insure_name;
}

public void setFarmer_insure_name(String farmer_insure_name) {
    this.farmer_insure_name = farmer_insure_name;
}

public String getFarmer_bank_hypo() {
    return farmer_bank_hypo;
}

public void setFarmer_bank_hypo(String farmer_bank_hypo) {
    this.farmer_bank_hypo = farmer_bank_hypo;
}

public String getFarmer_name() {
    return farmer_name;
}

public void setFarmer_name(String farmer_name) {
    this.farmer_name = farmer_name;
}

public String getVillage() {
    return village;
}

public void setVillage(String village) {
    this.village = village;
}

public String getTaluka() {
    return taluka;
}

public void setTaluka(String taluka) {
    this.taluka = taluka;
}

public String getDistrict() {
    return district;
}

public void setDistrict(String district) {
    this.district = district;
}

public String getTagging_date() {
    return tagging_date;
}

public void setTagging_date(String tagging_date) {
    this.tagging_date = tagging_date;
}

public String getTag_no() {
    return tag_no;
}

public void setTag_no(String tag_no) {
    this.tag_no = tag_no;
}

public String getAnimal_species() {
    return animal_species;
}

public void setAnimal_species(String animal_species) {
    this.animal_species = animal_species;
}

public String getAnimal_breed() {
    return animal_breed;
}

public void setAnimal_breed(String animal_breed) {
    this.animal_breed = animal_breed;
}

public String getBody_color() {
    return body_color;
}

public void setBody_color(String body_color) {
    this.body_color = body_color;
}

public String getShape_right() {
    return shape_right;
}

public void setShape_right(String shape_right) {
    this.shape_right = shape_right;
}

public String getShape_left() {
    return shape_left;
}

public void setShape_left(String shape_left) {
    this.shape_left = shape_left;
}

public String getTail_switch() {
    return tail_switch;
}

public void setTail_switch(String tail_switch) {
    this.tail_switch = tail_switch;
}

public String getOther_marks() {
    return other_marks;
}

public void setOther_marks(String other_marks) {
    this.other_marks = other_marks;
}

public String getPrag_status() {
    return prag_status;
}

public void setPrag_status(String prag_status) {
    this.prag_status = prag_status;
}

public String getLactations() {
    return lactations;
}

public void setLactations(String lactations) {
    this.lactations = lactations;
}

public String getMilk_qty() {
    return milk_qty;
}

public void setMilk_qty(String milk_qty) {
    this.milk_qty = milk_qty;
}

public String getSum_insured() {
    return sum_insured;
}

public void setSum_insured(String sum_insured) {
    this.sum_insured = sum_insured;
}

public String getTag_photo() {
    return tag_photo;
}

public void setTag_photo(String tag_photo) {
    this.tag_photo = tag_photo;
}

public String getHead_photo() {
    return head_photo;
}

public void setHead_photo(String head_photo) {
    this.head_photo = head_photo;
}

public String getLeft_photo() {
    return left_photo;
}

public void setLeft_photo(String left_photo) {
    this.left_photo = left_photo;
}

public String getRight_photo() {
    return right_photo;
}

public void setRight_photo(String right_photo) {
    this.right_photo = right_photo;
}

public String getFarmer_photo() {
    return farmer_photo;
}

public void setFarmer_photo(String farmer_photo) {
    this.farmer_photo = farmer_photo;
}

}

DatabaseHelper类

public class DatabaseHelper extends SQLiteOpenHelper {


private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

public static final String DATABASE_NAME = "SNVINSURANCE.db";
public static final String TABLE_NAME_IMAGES = "images_table";

private static final int DATABASE_VERSION = 1;

private static final String IMAGES_TABLE = "ImagesTable";


public static final String COLUMN_ID = "ID";
public static final String BANK_COLUMN = "BANK_NAME";
public static final String INSURED_COLUMN = "INSURED_NAME";
public static final String BNAKHYPO_COLUMN = "BANKHYPO_NAME";
public static final String COLUMN_STATUS = "status";


public static final String FARMERNAME_ID = "ID";
public static final String SNV_ID = "SNV";

public static final String FARMERNAME_COLUMN = "FARMER_NAME";
public static final String VILLAGE_COLUMN = "VILLAGE";
public static final String TALUKA_COLUMN = "TALUKA";
public static final String DISTRICT_COLUMN = "DISTRICT";
public static final String TAGGING_DATE_COLUMN = "TAGGING_DATE";

public static final String ANIMAL_ID = "ID";
public static final String TAG_COLUMN = "TAG";
public static final String ANIMAL_SPECIES_COLUMN = "ANIMAL_SPECIES";
public static final String ANIMAL_BREED_COLUMN = "ANIMAL_BREED";
public static final String ANIMAL_BODY_COLOR_COLUMN = "ANIMAL_BODY_COLOR";
public static final String ANIMAL_SHAPE_RIGHT_COLUMN = "ANIMAL_SHAPE_RIGHT";
public static final String ANIMAL_SHAPE_LEFT_COLUMN = "ANIMAL_SHAPE_LEFT";
public static final String ANIMAL_SWITCH_OF_TAIL_COLUMN = "ANIMAL_SWITCH_OF_TAIL";
public static final String AGE_COLUMN = "AGE";
public static final String ANIMAL_OTHER_MARKS_COLUMN = "ANIMAL_OTHER_MARKS";
public static final String PRAG_STATUS_COLUMN = "PRAG_STATUS";
public static final String NUMBER_OF_LACTATION_COLUMN = "NUMBER_OF_LACTATION";
public static final String CURRENT_MILK_COLUMN = "CURRENT_MILK";
public static final String SUM_INSURED_COLUMN = "SUM_INSURED";


public static final String TAG_IMAGE_COLUMN = "TAG_IMAGE";
public static final String HEAD_IMAGE_COLUMN = "HEAD_IMAGE";
public static final String LEFT_SIDE_IMAGE_COLUMN = "LEFT_SIDE_IMAGE";
public static final String RIGHT_SIDE_IMAGE_COLUMN = "RIGHT_SIDE_IMAGE";
public static final String TAIL_IMAGE_COLUMN = "TAIL_IMAGE";
public static final String IDPROOF_IMAGE_COLUMN = "IDPROOF_IMAGE";


public DatabaseHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table " + TABLE_NAME_IMAGES + "(BANK_ID INTEGER PRIMARY KEY AUTOINCREMENT,BANK_NAME TEXT,INSURED_NAME TEXT,BANKHYPO_NAME TEXT,FARMER_NAME TEXT,VILLAGE TEXT,TALUKA TEXT,DISTRICT TEXT,TAGGING_DATE DATE, TAG NUMBER,ANIMAL_SPECIES TEXT,ANIMAL_BREED TEXT,ANIMAL_BODY_COLOR TEXT,ANIMAL_SHAPE_RIGHT TEXT,ANIMAL_SHAPE_LEFT TEXT,ANIMAL_SWITCH_OF_TAIL TEXT,AGE NUMBER,ANIMAL_OTHER_MARKS TEXT,PRAG_STATUS TEXT,NUMBER_OF_LACTATION NUMBER,CURRENT_MILK NUMBER,SUM_INSURED NUMBER,TAG_IMAGE BLOB NOT NULL,HEAD_IMAGE BLOB NOT NULL,LEFT_SIDE_IMAGE BLOB NOT NULL,RIGHT_SIDE_IMAGE BLOB NOT NULL,TAIL_IMAGE BLOB NOT NULL,IDPROOF_IMAGE BLOB NOT NULL)");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE  IF EXISTS " + TABLE_NAME_IMAGES);

    onCreate(db);


}


public boolean insertImage(String bank, String insurename, String bankhypo, String farmername, String village, String taluka, String district, String tagging_date, String tag, String animal_species, String animal_breeds, String animal_body_color, String shape_right, String shape_left, String tail, String age, String marks_other, String prag, String lactation, String current_milk, String sum, byte[] tag_image, byte[] head_image, byte[] left_image, byte[] right_image, byte[] tail_image, byte[] id_proof_image) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put(BANK_COLUMN, bank);
    contentValues.put(INSURED_COLUMN, insurename);
    contentValues.put(BNAKHYPO_COLUMN, bankhypo);


    contentValues.put(FARMERNAME_COLUMN, farmername);
    contentValues.put(VILLAGE_COLUMN, village);
    contentValues.put(TALUKA_COLUMN, taluka);
    contentValues.put(DISTRICT_COLUMN, district);
    contentValues.put(TAGGING_DATE_COLUMN, tagging_date);

    contentValues.put(TAG_COLUMN, tag);
    contentValues.put(ANIMAL_SPECIES_COLUMN, animal_species);
    contentValues.put(ANIMAL_BREED_COLUMN, animal_breeds);
    contentValues.put(ANIMAL_BODY_COLOR_COLUMN, animal_body_color);
    contentValues.put(ANIMAL_SHAPE_RIGHT_COLUMN, shape_right);
    contentValues.put(ANIMAL_SHAPE_LEFT_COLUMN, shape_left);
    contentValues.put(ANIMAL_SWITCH_OF_TAIL_COLUMN, tail);
    contentValues.put(AGE_COLUMN, age);
    contentValues.put(ANIMAL_OTHER_MARKS_COLUMN, marks_other);
    contentValues.put(PRAG_STATUS_COLUMN, prag);
    contentValues.put(NUMBER_OF_LACTATION_COLUMN, lactation);
    contentValues.put(CURRENT_MILK_COLUMN, current_milk);
    contentValues.put(SUM_INSURED_COLUMN, sum);

    contentValues.put(TAG_IMAGE_COLUMN, tag_image);
    contentValues.put(HEAD_IMAGE_COLUMN, head_image);
    contentValues.put(LEFT_SIDE_IMAGE_COLUMN, left_image);
    contentValues.put(RIGHT_SIDE_IMAGE_COLUMN, right_image);
    contentValues.put(TAIL_IMAGE_COLUMN, tail_image);
    contentValues.put(IDPROOF_IMAGE_COLUMN, id_proof_image);

    long result = db.insert(TABLE_NAME_IMAGES, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;

}

public DatabaseHelper open() throws SQLException {
    SQLiteDatabase db = this.getWritableDatabase();
    return this;
}


public List<DataModel> getdata() {
    // DataModel dataModel = new DataModel();
    List<DataModel> data = new ArrayList<>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select BANK_ID,BANK_NAME,INSURED_NAME,BANKHYPO_NAME,FARMER_NAME,VILLAGE,TALUKA,DISTRICT,TAGGING_DATE from " + TABLE_NAME_IMAGES + " ;", null);
    StringBuffer stringBuffer = new StringBuffer();
    DataModel dataModel = null;
    while (cursor.moveToNext()) {
        dataModel = new DataModel();

        String BANK_ID = cursor.getString(cursor.getColumnIndexOrThrow("BANK_ID"));
        String name = cursor.getString(cursor.getColumnIndexOrThrow("BANK_NAME"));
        String country = cursor.getString(cursor.getColumnIndexOrThrow("INSURED_NAME"));
        String city = cursor.getString(cursor.getColumnIndexOrThrow("BANKHYPO_NAME"));

        String FARMER_NAME = cursor.getString(cursor.getColumnIndexOrThrow("FARMER_NAME"));
        String VILLAGE = cursor.getString(cursor.getColumnIndexOrThrow("VILLAGE"));
        String TALUKA = cursor.getString(cursor.getColumnIndexOrThrow("TALUKA"));

        String DISTRICT = cursor.getString(cursor.getColumnIndexOrThrow("DISTRICT"));
        String TAGGING_DATE = cursor.getString(cursor.getColumnIndexOrThrow("TAGGING_DATE"));

        dataModel.setBANK_ID(BANK_ID);
        dataModel.setId(name);
        dataModel.setFarmer_insure_name(city);
        dataModel.setFarmer_bank_hypo(country);

        dataModel.setFarmer_name(FARMER_NAME);
        dataModel.setVillage(VILLAGE);
        dataModel.setTaluka(TALUKA);

        dataModel.setDistrict(DISTRICT);
        dataModel.setTagging_date(TAGGING_DATE);


        stringBuffer.append(dataModel);
        // stringBuffer.append(dataModel);
        data.add(dataModel);
    }

    for (DataModel mo : data) {

        Log.e("Hellomo", "" + mo.getFarmer_insure_name());
    }

    //

    return data;
}

}

首次活动课程

public class SyncAllActivity extends AppCompatActivity {

Cursor model = null;
Cursor mode = null;
MyCustomAdapter adapter = null;
Button show;
DatabaseHelper database;
private RecyclerView recyclerview;
RecycleAdapter recycler;
List<DataModel> datamodel;
private ProgressDialog pdialog;
Context context;
private RecyclerView.LayoutManager mLayoutManager;
private MyCustomAdapter myCustomAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sync_all_activity);

    context = this;

    datamodel = new ArrayList<DataModel>();
    recyclerview = findViewById(R.id.recycle);

    database = new DatabaseHelper(SyncAllActivity.this);
    datamodel = database.getdata();
    recycler = new RecycleAdapter(datamodel);


    Log.e("data", "" + datamodel);

    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerview.setLayoutManager(layoutManager);
    recyclerview.setHasFixedSize(true);
    myCustomAdapter = new MyCustomAdapter(datamodel);
    recyclerview.setAdapter(myCustomAdapter);


}



public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomAdapter.MyViewHolder> {

    List<DataModel> moviesList;



        public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView bank_company, insured_name, bank_hypo;
            CardView cardView;
            MaterialRippleLayout next_button_main_activity;


            public MyViewHolder(View view) {
                super(view);
                context = itemView.getContext();
                bank_company = itemView.findViewById(R.id.tvInsuranceList);
                insured_name = itemView.findViewById(R.id.tvInsuredName);
                bank_hypo = itemView.findViewById(R.id.tvBankHypo);

                cardView = itemView.findViewById(R.id.cardViewMain);
                next_button_main_activity = itemView.findViewById(R.id.next_button_main_activity);

            }


        }


        public MyCustomAdapter(List<DataModel> moviesList) {
            this.moviesList = moviesList;
        }


        @Override
        public MyCustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.sam1
                            , parent, false);

            return new MyCustomAdapter.MyViewHolder(itemView);

        }

        public void clear() {
            int size = this.moviesList.size();
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    this.moviesList.remove(0);
                }

                this.notifyItemRangeRemoved(0, size);
            }
        }


        @Override
        public void onBindViewHolder(final MyCustomAdapter.MyViewHolder holder, final int position) {


            final DataModel dataModel = moviesList.get(position);

            holder.bank_company.setText(dataModel.getId());
            holder.insured_name.setText(dataModel.getFarmer_insure_name());
            holder.bank_hypo.setText(dataModel.getFarmer_bank_hypo());

            holder.cardView.setVisibility(View.VISIBLE);



            holder.next_button_main_activity.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    Intent intent = new Intent(context, SyncFarmerActivity.class);
                    intent.putExtra("BANK_ID", moviesList.get(position).getBANK_ID() + "");
                    Log.e("id", moviesList.get(position).getBANK_ID() + "");
                    startActivity(intent);

                }
            });
        }


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

    }


}

第二个活动班级

public class SyncFarmerActivity extends AppCompatActivity {



Button show;
DatabaseHelper database;
private RecyclerView recyclerview;
RecycleAdapter recycler;
List<DataModel> datamodel;
String id;
Context context;
private RecyclerView.LayoutManager mLayoutManager;
private MyCustomAdapter myCustomAdapter;
DataModel dataModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sync_farmer);

    context = this;


    Intent i = getIntent();
    id = i.getStringExtra("BANK_ID");
    Log.e("BANK_ID", id + "");



    datamodel = new ArrayList<DataModel>();
    recyclerview = findViewById(R.id.recycle);

    database = new DatabaseHelper(SyncFarmerActivity.this);
    datamodel = database.getdata();

    recycler = new RecycleAdapter(datamodel);


    Log.e("HIteshdata", "" + datamodel);

    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerview.setLayoutManager(layoutManager);
    recyclerview.setHasFixedSize(true);
    myCustomAdapter = new MyCustomAdapter(datamodel);
    recyclerview.setAdapter(myCustomAdapter);

}

public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomAdapter.MyViewHolder> {

    List<DataModel> moviesList;


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvFarmerName, tvCity, tvTaluka, farmer_name, tvDistrict, tvTaggingDate, district, tagging_date;

        CardView cardView;
        MaterialRippleLayout next_button_main_activity;


        public MyViewHolder(View view) {
            super(view);
            context = itemView.getContext();
            tvFarmerName = itemView.findViewById(R.id.tvFarmerName);
            tvCity = itemView.findViewById(R.id.tvCity);
            tvTaluka = itemView.findViewById(R.id.tvTaluka);

            tvDistrict = itemView.findViewById(R.id.tvDistrict);

            tvTaggingDate = itemView.findViewById(R.id.tvTaggingDate);


        }


    }


    public MyCustomAdapter(List<DataModel> moviesList) {
        this.moviesList = moviesList;
    }

    @Override
    public MyCustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sam
                        , parent, false);

        return new MyCustomAdapter.MyViewHolder(itemView);
    }

    public void clear() {
        int size = this.moviesList.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                this.moviesList.remove(0);
            }

            this.notifyItemRangeRemoved(0, size);
        }
    }


    @Override
    public void onBindViewHolder(MyCustomAdapter.MyViewHolder holder, final int position) {


        DataModel dataModel = moviesList.get(position);

        holder.tvFarmerName.setText(dataModel.getFarmer_name());
        holder.tvCity.setText(dataModel.getVillage());
        holder.tvTaluka.setText(dataModel.getTaluka());
        holder.tvDistrict.setText(dataModel.getDistrict());
        holder.tvTaggingDate.setText(dataModel.getTagging_date());


    }


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

}

}

任何人都可以帮助我.. 预先谢谢你。

1 个答案:

答案 0 :(得分:0)

您必须在Database类中创建另一个方法,以便从您要从第一个活动传递的特定ID中选择值,例如:

DatabaseHelper类

public List<DataModel> getSpecificData(String id) {
// DataModel dataModel = new DataModel();
List<DataModel> data = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select BANK_ID,BANK_NAME,INSURED_NAME,BANKHYPO_NAME,FARMER_NAME,VILLAGE,TALUKA,DISTRICT,TAGGING_DATE from " + TABLE_NAME_IMAGES + "WHERE BANK_ID = " + id + ";", null);
StringBuffer stringBuffer = new StringBuffer();
DataModel dataModel = null;
while (cursor.moveToNext()) {
  //Rest of your code
return data;
}

在第二个活动中使用此方法,例如

Intent i = getIntent();
id = i.getStringExtra("BANK_ID");

database = new DatabaseHelper(SyncFarmerActivity.this);
datamodel = database.getSpecificData(id);