آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

Search in sqlite and show in recycelerView

محمدحسن  8 سال پیش  8 سال پیش
0 0

سلام دوستان ایام بکام

من میخوام یک رکورد رو براساس نام در داخل دیتابیسم پیدا کنم و نمایش بدم تا حدودی موفق بودم چون رکورد برام پیدا میکنه ولی متاسفانه هیچکدوم ازمشخصات اون فیلد نیست و همه رو با null پر میکنه سورس کدهامو همراه با عکس امولاتورم میگذارم ی چند وقتی هست روش زومم ولی به نتیجه ای نرسیدم

package com.example.siba03133613004.test1;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

public class PersonDBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "people.db";
private static final int DATABASE_VERSION = 1 ;
public static final String TABLE_NAME = "People";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PERSON_NAME = "name";
public static final String COLUMN_PERSON_AGE = "age";
public static final String COLUMN_PERSON_OCCUPATION = "occupation";
public static final String COLUMN_PERSON_IMAGE = "image";


public PersonDBHelper(Context context) {
super(context,DATABASE_NAME, null,DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(" CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PERSON_NAME + " TEXT NOT NULL, " +
COLUMN_PERSON_AGE + " NUMBER NOT NULL, " +
COLUMN_PERSON_OCCUPATION + " TEXT NOT NULL, " +
COLUMN_PERSON_IMAGE + " BLOB NOT NULL);"
);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
this.onCreate(db);
}

public void saveNewPerson(Person person) {

SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_PERSON_NAME, person.getName());
values.put(COLUMN_PERSON_AGE, person.getAge());
values.put(COLUMN_PERSON_OCCUPATION, person.getOccupation());
values.put(COLUMN_PERSON_IMAGE, person.getImage());

// insert
db.insert(TABLE_NAME,null, values);
db.close();
}
public List<Person> peopleList(String filter) {
String query;
if(filter.equals("")){
//regular query
query = "SELECT * FROM " + TABLE_NAME;
}else{
//filter results by filter option provided
query = "SELECT * FROM " + TABLE_NAME + " ORDER BY "+ filter;
}

List<Person> personLinkedList = new LinkedList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Person person;

if (cursor.moveToFirst()) {
do {
person = new Person();

person.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
person.setName(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_NAME)));
person.setAge(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_AGE)));
person.setOccupation(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_OCCUPATION)));
person.setImage(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_IMAGE)));
personLinkedList.add(person);
} while (cursor.moveToNext());
}


return personLinkedList;
}
public Person getPerson(long id){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE _id="+ id;
Cursor cursor = db.rawQuery(query, null);

Person receivedPerson = new Person();
if(cursor.getCount() > 0) {
cursor.moveToFirst();

receivedPerson.setName(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_NAME)));
receivedPerson.setAge(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_AGE)));
receivedPerson.setOccupation(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_OCCUPATION)));
receivedPerson.setImage(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_IMAGE)));
}



return receivedPerson;


}
public List<Person> getPersonNamelist(String name) {
SQLiteDatabase db = this.getWritableDatabase();
List<Person> personLinkedList = new LinkedList<>();

String query = "SELECT * FROM " + TABLE_NAME + " WHERE name=" + "'name'";
Cursor cursor = db.rawQuery(query, null);

Person receivedPerson = new Person();
if (cursor.getCount() > 0) {
cursor.moveToFirst();

receivedPerson.setName(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_NAME)));
receivedPerson.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_PERSON_NAME)));
receivedPerson.setAge(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_AGE)));
receivedPerson.setOccupation(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_OCCUPATION)));
receivedPerson.setImage(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_IMAGE)));
}

personLinkedList.add(receivedPerson);


return personLinkedList;
}

public void deletePersonRecord(long id, Context context) {
SQLiteDatabase db = this.getWritableDatabase();

db.execSQL("DELETE FROM "+TABLE_NAME+" WHERE _id='"+id+"'");
Toast.makeText(context, "Deleted successfully.", Toast.LENGTH_SHORT).show();

}
public void updatePersonRecord(long personId, Context context, Person updatedperson) {
SQLiteDatabase db = this.getWritableDatabase();
//you can use the constants above instead of typing the column names
db.execSQL("UPDATE "+TABLE_NAME+" SET name ='"+ updatedperson.getName() + "', age ='" + updatedperson.getAge()+ "', occupation ='"+ updatedperson.getOccupation() + "', image ='"+ updatedperson.getImage() + "' WHERE _id='" + personId + "'");
Toast.makeText(context, "Updated successfully.", Toast.LENGTH_SHORT).show();


}
}
package com.example.siba03133613004.test1;

import android.content.Intent;
import android.support.v4.view.MenuItemCompat;
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.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private Button btnSerach;
private TextView txtSearch;
private PersonDBHelper dbHelper;
private PersonAdapter adapter;
private String filter = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize the variables
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
btnSerach=(Button)findViewById(R.id.btnSearch);
txtSearch=(TextView)findViewById(R.id.txtSearch);
btnSerach.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SearchResult(txtSearch.getText().toString());
}
});
//populate recyclerview
populaterecyclerView(filter);
}
private void SearchResult(String SearchText){
dbHelper = new PersonDBHelper(this);
adapter=new PersonAdapter(dbHelper.getPersonNamelist(SearchText),this,mRecyclerView);
mRecyclerView.setAdapter(adapter);
}
private void populaterecyclerView(String filter){
dbHelper = new PersonDBHelper(this);
adapter = new PersonAdapter(dbHelper.peopleList(filter), this, mRecyclerView);
mRecyclerView.setAdapter(adapter);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);

MenuItem item = menu.findItem(R.id.filterSpinner);
Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);

final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.filterOptions, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String filter = parent.getSelectedItem().toString();
populaterecyclerView(filter);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
populaterecyclerView(filter);
}
});


spinner.setAdapter(adapter);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.addMenu:
goToAddUserActivity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

private void goToAddUserActivity(){
Intent intent = new Intent(MainActivity.this, AddRecordActivity.class);
startActivity(intent);
}

@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}


}
 برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال 
k112  8 سال پیش
+2 0

سلام

خوندن این کد خیلی سخت بود ولی مشکل شما در getPersonNamelist هست

  String query = "SELECT  * FROM " + TABLE_NAME + " WHERE name='" + name + "'";
0 0
داداش ی دنیا ممنون کارت درسته (8 سال پیش)

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .