在使用存储在 JSON 上的 ID 进行 SearchView 过滤后,如何从 ListView 打开 Activity
Posted
技术标签:
【中文标题】在使用存储在 JSON 上的 ID 进行 SearchView 过滤后,如何从 ListView 打开 Activity【英文标题】:How can I open a Activity from a ListView after SearchView filter using an ID stored on a JSON 【发布时间】:2019-11-10 20:37:00 【问题描述】:我是 java 新手,我正在做一个应用程序来完成我的大学。这个应用程序在从 JSON 获取数据的 ListView 上有一个 SearchView 过滤。我几乎完成了,但我在过滤器部分之后卡住了。
在 SearchBar 上输入并过滤返回结果后,我无法打开从 JSON 获取 ID 的新意图。我只能使用位置 ListView 编号打开意图(由于过滤过程,这对我的需要不起作用)。
下图准确地显示了问题: Image
所以我的必要性是:使用存储在 JSON 中的 ID 打开一个意图,以避免过滤结果后更改位置 ID 的问题。
我不知道如何解决这个问题。
豆子:
public class Model
private String description;
private int id;
public Model(int id, String description)
this.setId(id);
this.setDescription(description);
public String getDescription()
return description;
public void setDescription(String description)
this.description = description;
public int getId()
return id;
public void setId(int id)
this.id = id;
主要:
public class MainActivity extends Activity implements SearchView.OnQueryTextListener, SearchView.OnCloseListener
private ListView list;
private ListViewAdapter adapter;
private SearchView editsearch;
public static ArrayList<Model> modelArrayList = new ArrayList<Model>();
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listview);
loadDataJSON();
adapter = new ListViewAdapter(this);
list.setAdapter(adapter);
editsearch = (SearchView) findViewById(R.id.search);
editsearch.setOnQueryTextListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
int a = position;
Intent myIntent = new Intent(MainActivity.this, Info.class);
myIntent.putExtra("valor", a);
startActivity(myIntent);
);
public String loadJSON(String arq)
String json = null;
try
InputStream is = getAssets().open(arq);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
catch (IOException ex)
ex.printStackTrace();
return null;
return json;
private void loadDataJSON()
String arquivo = loadJSON("lista.json");
try
JSONObject obj = new JSONObject(arquivo);
JSONArray a = obj.getJSONArray("locais");
for (int i = 0; i < a.length(); i++)
JSONObject item = a.getJSONObject(i);
Model model = new Model(item.getInt("id"),item.getString("description"));
modelArrayList.add(model);
catch (JSONException e)
throw new RuntimeException(e);
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
String text = newText;
adapter.filter(text);
return false;
@Override
public boolean onClose()
// TODO Auto-generated method stub
return false;
适配器:
public class ListViewAdapter extends BaseAdapter
Context mContext;
LayoutInflater inflater;
public ArrayList<Model> arraylist;
public ListViewAdapter(Context context )
mContext = context;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<Model>();
this.arraylist.addAll(MainActivity.modelArrayList);
public class ViewHolder
TextView name;
@Override
public int getCount()
return MainActivity.modelArrayList.size();
@Override
public Model getItem(int position)
return MainActivity.modelArrayList.get(position);
@Override
public long getItemId(int position)
return position;
public View getView(final int position, View view, ViewGroup parent)
final ViewHolder holder;
if (view == null)
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
holder.name = (TextView) view.findViewById(R.id.name);
view.setTag(holder);
else
holder = (ViewHolder) view.getTag();
holder.name.setText(MainActivity.modelArrayList.get(position).getDescription());
return view;
public void filter(String charText)
charText = charText.toLowerCase(Locale.getDefault());
MainActivity.modelArrayList.clear();
if (charText.length() == 0)
MainActivity.modelArrayList.addAll(arraylist);
else
for (Model mp : arraylist)
if (mp.getDescription().toLowerCase(Locale.getDefault()).contains(charText))
MainActivity.modelArrayList.add(mp);
notifyDataSetChanged();
信息:
public class Info extends Activity
TextView aaa, bbb;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
aaa = (TextView) findViewById(R.id.aaa);
bbb = (TextView) findViewById(R.id.bbb);
Intent mIntent = getIntent();
int id = mIntent.getIntExtra("valor",0);
String arquivo = loadJSON("lista.json");
try
JSONObject obj = new JSONObject(arquivo);
JSONArray a = obj.getJSONArray("locais");
JSONObject item = a.getJSONObject(id);
aaa.setText(item.getString("id"));
bbb.setText("Base: "+item.getString("description"));
catch (JSONException e)
throw new RuntimeException(e);
public String loadJSON(String arq)
String json = null;
try
InputStream is = getAssets().open(arq);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
catch (IOException ex)
ex.printStackTrace();
return null;
return json;
JSON:
“地区”:[ “id”:“1”, “描述”:“文本 A” , “id”:“2”, “描述”:“文本 B” ]
【问题讨论】:
【参考方案1】:请改用int a = modelArrayList.get(position).getId()
。所以你会从 JSON 中获取 id
【讨论】:
好的,所以这通过了 ID,但并没有解决整个问题。现在文本 A 打开文本 B 内容,文本 B 崩溃并返回超出范围。知道如何解决这个问题吗?更新:固定【参考方案2】:您需要在 ListViewAdapter 中使用该意图 希望它工作正常
【讨论】:
以上是关于在使用存储在 JSON 上的 ID 进行 SearchView 过滤后,如何从 ListView 打开 Activity的主要内容,如果未能解决你的问题,请参考以下文章