我是Java的新用户,我正在做一个应用程序以完成我的大学。此应用程序对ListView进行SearchView过滤,以从JSON获取数据。我快完成了,但是卡在过滤器部分后面。
在SearchBar和过滤器上输入后,返回结果,我无法打开从JSON获取ID的新意图。我只能使用位置ListView数字打开意图(由于过滤过程,这对我来说是行不通的。)
下图准确显示了问题所在: Image
所以我的必要性是:使用存储在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”, “ description”:“文本A” },{ “ id”:“ 2”, “描述”:“文字B” }]}
答案 0 :(得分:0)
改为使用int a = modelArrayList.get(position).getId()
。这样您将从JSON获取ID
答案 1 :(得分:0)
您需要在ListViewAdapter中使用该意图 希望它能正常工作