我一直在关注几个教程,这些教程让我成功地从我的数据库中获取信息,从我的网站到我的应用程序,然后我可以在列表视图中显示它,但我想要做的不是显示完整列表,我想有几个按钮让我只显示符合某些标准的数据,比如底部 1 只显示 medio=Periodicos 的列表项,按钮 2 显示 medio=Radios 的项目 这是我正在使用的代码
在我的主要活动中
FancyAdapter aa = null;
static ArrayList<String> resultRow;
//after onCreate
//this button I'm usin now but I'm getting the hole list!
Button periodicos = (Button) findViewById(R.id.btnPeriodicos);
periodicos.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
weballamar = "http://www.mysite.com/mydbjsonstring.php";
webcall();
}});
//webcall gets the info from the internet and displays the list
public void webcall(){
ListView myListView = (ListView) findViewById(R.id.mylistView);
aa = new FancyAdapter();
myListView.setOnItemClickListener(onPeriodicosListClick);
myListView.setAdapter(aa);
PeriodicosListLayout.setVisibility(View.VISIBLE);
webLayout.setVisibility(View.GONE);
try {
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(weballamar);
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream webs = entity.getContent();
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(webs, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
webs.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
webResult resultRow = new webResult();
resultRow._id = json_data.getString("id");
resultRow.Name = json_data.getString("Id_Nombre");
resultRow.Medio = json_data.getString("Medio");
resultRow.Pais = json_data.getString("Pais");
resultRow.Estado = json_data.getString("Estado");
resultRow.Ciudad = json_data.getString("Ciudad");
resultRow.website = json_data.getString("website");
resultRow.Url = json_data.getString("Url");
resultRow.Url2 = json_data.getString("Url2");
resultRow.InfAnex = json_data.getString("InfAnex");
arrayOfWebData.add(resultRow);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
} catch (Exception e) {
// this is the line of code that sends a real error message to the
// log
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
// this is the line that prints out the location in
// the code where the error occurred.
e.printStackTrace();
}
}
class FancyAdapter extends ArrayAdapter<webResult> {
FancyAdapter() {
super(mainActivity.this,
android.R.layout.simple_list_item_1, arrayOfWebData);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.listitem, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(arrayOfWebData.get(position));
return (convertView);
}
}
class ViewHolder {
public TextView name = null;
public TextView estado = null;
public TextView ciudad = null;
ViewHolder(View row) {
name = (TextView) row.findViewById(R.id.periodicoName);
estado = (TextView) row.findViewById(R.id.periodicoEstado);
ciudad = (TextView) row.findViewById(R.id.periodicoCiudad);
}
void populateFrom(webResult r) {
name.setText(r.Name);
estado.setText(r.Estado);
ciudad.setText(r.Ciudad);
}
}
如您所见,我在列表视图中显示了名称庄园和城市的完整列表,但该列表包含的项目在 medio 列中要么是周期性的要么是无线电的,所以我怎样才能让我的适配器区分并选择显示只有那些女巫 medio=periodico,从那里我可以做相反的事情来选择 medio=radio Tahnks