如何使用存储在JSON上的ID之后的搜索视图过滤器中从listView打开活动



im在java上的新事物,我正在为我的大学做一个应用程序。该应用程序在ListView上具有从JSON获取数据的搜索视图过滤。我几乎完成了,但是我在过滤器部分之后卡住了。

在搜索栏上输入并返回结果后,我无法打开一个新的意图从JSON获取ID。我只能使用位置listView编号打开意图(由于过滤过程而对我的必要性无效(。

下图完全显示了问题:图像

因此,我的必要性是:使用存储在JSON上的ID打开意图,以避免更改过滤器结果后的位置ID的问题。

我不知道如何解决这个问题。

bean:

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:

{" locais":[{{ " id":" 1", "描述":"文本A" },{ " id":" 2", "描述":"文本B" }]}

而不是使用 int a = modelArrayList.get(position).getId()。因此,您将从JSON获得ID

您需要在ListViewAdapter中使用该意图希望它的工作正常

相关内容

  • 没有找到相关文章

最新更新