搜索列表视图项并打开一个活动

时间:2016-10-18 10:11:25

标签: string listview search android-activity android-edittext

我想在列表视图上创建一个搜索功能,当我输入一些文本时,项目会被过滤,当我点击某个项目时,它会转到另一个活动。

我有一个编辑文本和一个列表视图和一个数组字符串,它们是与我的活动同名的字符串项目,我想在点击并搜索列表视图项目后打开它。

我的代码:

输入代码

package com.shady.letsdoit; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import java.util.ArrayList; import java.util.HashMap; public class Search extends AppCompatActivity { private EditText search_box; private ArrayAdapter<String> listAdapter; ArrayList<HashMap<String, String>> productList; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search); getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(true); search_box = (EditText) findViewById(R.id.search_box); final ListView list_view = (ListView) findViewById(R.id.list_view); String products[] = getResources().getStringArray(R.array.search); /** <string-array name="search"> <item>@string/Android_whats_android</item> <item>@string/Android_perso_wallpaper</item> <item>@string/Android_perso_ringtone</item> <item>@string/Android_perso_lockscreen</item> <item>@string/Android_perso_langueg</item> <item>@string/Android_perso_dualsim</item> <item>@string/Android_perso_datetime</item> <item>@string/Android_perso_bluetooth</item> <item>@string/Android_perso_appmanagre</item> <item>@string/Android_perso_accounts</item> <item>@string/Android_f_m</item> <item>@string/Android_insta</item> <item>@string/Android_telgram</item> <item>@string/Android_bestapp</item> <item>@string/Android_root</item> <item>@string/Pc_whats_win</item> <item>@string/Pc_boot</item> <item>@string/Pc_nasb_win_7</item> <item>@string/Pc_nasb_win_8</item> <item>@string/Pc_nasb_win_10</item> <item>@string/Pc_win7_perso</item> <item>@string/Pc_win10_perso</item> <item>@string/Pc_win_internet_browser</item> <item>@string/Pc_win_internet_download</item> <item>@string/Pc_win_safe</item> <item>@string/Pc_win_errors_softwere</item> <item>@string/Pc_win_errors_hardwere</item> <item>@string/Pc_win_bestsoftweres</item> </string-array> **/ listAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.subject_name, products); list_view.setAdapter(listAdapter); list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String selectedItem = (String) parent.getItemAtPosition(position); if (selectedItem.equals("@string/Android_perso_lockscreen")){ /**each and every single of these "@string..." are an Activity for example * @string/Android_perso_wallpaper = Android_perso_wallpaper.class * that I want to open it when I click on same Item * I tried some methods but it didn't work plz help me thanks. */ Intent intent = new Intent(Search.this, Android_perso_lockscreen.class); startActivity(intent); } } }); search_box.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { Search.this.listAdapter.getFilter().filter(cs); } @Override public void afterTextChanged(Editable editable) { } }); } /**my action bar **/ @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.actionbar,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId()) { case R.id.action_search: Intent search = new Intent(this, Search.class); search.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(search); return true; case R.id.action_home: Intent home = new Intent(this, MainActivity.class); home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(home); return true; case R.id.action_fav: Intent favorite = new Intent(this, Favorits.class); favorite.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(favorite); return true; case R.id.action_phone: Intent phone = new Intent(this, phon.class); phone.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(phone); return true; case R.id.action_pc: Intent pc = new Intent(this, pc.class); pc.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(pc); return true; } return false; }}

0 个答案:

没有答案