جست و جو در لیست ویو
سلام.
تقریبا قبلا هم چنین سوالی ، با اندکی تغییر نوشتم.
اما چون یکم فرق داشت دوباره مطرح میکنم
ببینید من تو کلاس G دستوری نوشتم که تیتر از پایگاه داده بخونه و لیست ویو م رو پر کنه
بالای لیست ویو یه ادیت باکس گذاشتم و کنارش دکمه ی سرچ قرار دادم.
حالا دستور این دکمه باید چی باشه و کجا تعریف بشه؟؟؟


من چنین کدی نوشتم. کرش نمیکنه . اما گیر میکنه و بعد از چند دقیقه میره صفحه اصلی
EditText edtSearch = (EditText) findViewById(R.id.edtSearch); final String key = edtSearch.getText().toString(); Button btnSearch = (Button) findViewById(R.id.btnSearch); btnSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Cursor cursor = G.database.rawQuery("SELECT * FROM content WHERE title LIKE '%" + key + "%'", null); while (cursor.moveToFirst()) { Struct item = new Struct(); item.title = cursor.getString(cursor.getColumnIndex("title")); item.content = cursor.getString(cursor.getColumnIndex("content")); lists.add(item); } cursor.close(); adapter.notifyDataSetChanged(); } });

تصحیح کنید
while (cursor.moveToNext()) { Struct item = new Struct(); item.title = cursor.getString(cursor.getColumnIndex("title")); item.content = cursor.getString(cursor.getColumnIndex("content"));
Log.v("iLOG" , "title : " + Item.title);
Log.v("iLOG" , "content : " + Item.content); lists.add(item); }
این رو امتحان کنید
چون کدی که شما نوشتین تعیین می کنه تا ابد توی چرخه بی افته

اون کدت یک کتیشن کم داره این کد منو بزن همه برنامه هام با این کده حل میشه input هم که ورودی تکست باکسته
"SELECT * FROM table where (table.title like'" + '%' + input + "%') "

کلاس G
public class G extends Application { public static Context context; public static LayoutInflater inflater; public static Activity CurrentActivity; public static LocationManager locationManager; public static SQLiteDatabase database; public static final Handler HANDLER = new Handler(); public static ArrayList lists = new ArrayList(); public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath(); public static final String DIR_APP = DIR_SDCARD + "/database"; @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); new File(DIR_APP).mkdirs(); File file = new File(DIR_APP + "/database"); if ( !file.exists()) { copydb(); } database = SQLiteDatabase.openOrCreateDatabase(DIR_APP + "/database", null); Cursor cursor = database.rawQuery("SELECT * FROM content", null); while (cursor.moveToNext()) { try { Struct item = new Struct(); item.title = cursor.getString(cursor.getColumnIndex("title")); item.content = cursor.getString(cursor.getColumnIndex("content")); lists.add(item); } catch (Exception e) { } } cursor.close(); } private void copydb() { AssetManager assetManager = context.getAssets(); InputStream inputStream; try { inputStream = assetManager.open("Context"); HelperIO.copyFile(inputStream, DIR_APP + "/database"); } catch (IOException e) { e.printStackTrace(); } } }
کلاس مکانها
public class Place extends Activity { public ArrayAdapter adapter; public static ArrayList lists = new ArrayList(); @Override protected void onResume() { G.CurrentActivity = this; super.onResume(); if (adapter != null) { adapter.notifyDataSetChanged(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.place); final ListView lstContent = (ListView) findViewById(R.id.lstContent); Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate); lstContent.setLayoutAnimation(new LayoutAnimationController(animation)); adapter = new AdapterList(G.lists); lstContent.setAdapter(adapter); EditText edtSearch = (EditText) findViewById(R.id.edtSearch); final String key = edtSearch.getText().toString(); Button btnSearch = (Button) findViewById(R.id.btnSearch); btnSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Cursor cursor = G.database.rawQuery("SELECT * FROM content WHERE title = " + key, null); adapter.clear(); while (cursor.moveToNext()) { Struct item = new Struct(); item.title = cursor.getString(cursor.getColumnIndex("title")); item.content = cursor.getString(cursor.getColumnIndex("content")); Log.v("iLOG", "title : " + item.title); Log.v("iLOG", "content : " + item.content); lists.add(item); } cursor.close(); adapter.notifyDataSetChanged(); } }); } }

Try It :
btnSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Cursor cursor = G.database.rawQuery("SELECT * FROM content WHERE title = " + key, null); adapter.clear(); while (cursor.moveToNext()) { Struct item = new Struct(); item.title = cursor.getString(cursor.getColumnIndex("title")); item.content = cursor.getString(cursor.getColumnIndex("content")); Log.v("iLOG", "title : " + item.title); Log.v("iLOG", "content : " + item.content); G.lists.add(item); // Changed } cursor.close(); adapter.notifyDataSetChanged(); } });

جای تعریف متغیر ها باید اصلاح بشه
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String key = edtSearch.getText().toString();
G.lists.clear();
Cursor cursor = G.database.rawQuery("SELECT * FROM content WHERE title LIKE '%" + key + "%'", null);
while (cursor.moveToNext())
{
Struct item = new Struct();
item.title = cursor.getString(cursor.getColumnIndex("title"));
item.content = cursor.getString(cursor.getColumnIndex("content"));
G.lists.add(item);
}
cursor.close();
adapter.notifyDataSetChanged();
}
});

سلام دوستان
امیدوارم بحث را تا این جا درست فهمیده باشم و جواب بی راهی ندهم
به نظر من نیاز نیست برای جستجو بر اساس یک فیلد شما هر بار یه کوئری از دیتابیس بزنید بهتر اینه که دو تا ArrayList در کلاس G تعریف کنی
public static ArrayList<StructCountry> Array = new ArrayList<StructCountry>();
public static ArrayList<StructCountry> Array_2;
حالا با یه کوئری Array را از اطلاعات پایگاه پر می کنی و بعدش Array_2 را از اطلاعات Array پر می کنی . و در نهایت لیست ویو هم با اطلاعات Array_2 پر میشه .
G.Array = new ArrayList<StructCountry>(G.Array_2);
adapter = new AdapterMartyr(G.Array_2);
lstContent.setAdapter(adapter);
adapter.notifyDataSetChanged();
حالا موقعی که کاربر برو روی plainText در جال وارد کردن عبارت هست سرچی انجام می ده و نتایج را نشون می ده .
edt1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
String searchString = edt1.getText().toString().trim();
int textLength = searchString.length();
G.Array_2.clear();
String Rsearch="";
for (int i = 0; i <= (G.Array.size() - 1); i++) {
if (seter == 1) {
Rsearch= G.Array.get(i).فیلد مورد نظر;
}
if (seter == 2) {
Rsearch= G.Array.get(i).فیلد مورد نظر;
}
if (seter == 3) {
Rsearch= G.Array.get(i).فیلد مورد نظر;
}
if (seter == 4) {
Rsearch= G.Array.get(i).فیلد مورد نظر;
}
if (seter == 5) {
Rsearch= G.Array.get(i).name;
}
if (textLength <= Rsearch.length()) {
if (searchString.equalsIgnoreCase(Rsearch.substring(0, textLength)))
G.Array_2.add(G.Array.get(i));
}
}
adapter.notifyDataSetChanged();
}
در واقع با این کار به جای اینکه هر بار کوئری در پایگاه زده بشه و نتایج در لیست ویو نمایش داده بشه ، ای سرچ درون Array انجام میشه و نتیجه در Array_2 ریخته مشه و در نهایت هم در لیست ویو نمایش داده می شود .
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .