【问题标题】:How to show only list view items which value is equal to a value in the Json如何仅显示值等于 Json 中的值的列表视图项
【发布时间】:2017-09-30 10:45:55
【问题描述】:

我正在创建一个应在列表视图中显示景点的应用程序。 数据是从 json 解析的。 在这个 json 中有一个列,它声明了视线所在的城市。 现在我想创建一种过滤器,它应该使用我的 Json 中的 City 值检查当前的 Cityname。 例如,柏林有一个景点,我现在的城市是柏林,列表视图应该显示它。如果用户在慕尼黑并且视线在柏林,则列表视图不应显示此项目。 我从 TextView 中的不同 Activity 获取当前 cityname 值。 这是我的 Listview 活动:

public class Locations extends AppCompatActivity implements AdapterView.OnItemClickListener {
ArrayList<productforloc> arrayList;
ListView lv;
private String TAG = Locations.class.getSimpleName();
private TextView addressField; //Add a new TextView to your activity_main to display the address
private LocationManager locationManager;
private String provider;
int i = 1;
private ProgressDialog pDialog;
String name;



// URL to get contacts JSON
private static String url = "http://partypeople.bplaced.net/maptest.json";

ArrayList<HashMap<String, String>> contactList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);
    Intent i = getIntent();
    String cityname = i.getExtras().getString("cityname");
    TextView city = (TextView) findViewById(R.id.ort);
    city.setText(cityname);
    pDialog = new ProgressDialog(Locations.this);
    pDialog.setMessage("Please wait...");
    pDialog.setCancelable(true);
    pDialog.show();
    arrayList = new ArrayList<>();
    lv = (ListView) findViewById(R.id.lv);
    lv.setOnItemClickListener((AdapterView.OnItemClickListener) this);





runOnUiThread(new Runnable() {
        @Override
        public void run() {
            new ReadJSON().execute(url);
        }
    });

final ImageButton filteropen = (ImageButton) findViewById(R.id.aufklaupen);
    filteropen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RelativeLayout filter = (RelativeLayout) findViewById(R.id.filterloc);
            filter.setVisibility(View.VISIBLE);
            ImageButton filterclose = (ImageButton) findViewById(R.id.zuklappen);
            filterclose.setVisibility(View.VISIBLE);
            filteropen.setVisibility(View.INVISIBLE);

        }
    });
final ImageButton filterclose = (ImageButton) findViewById(R.id.zuklappen);
    filterclose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RelativeLayout filter = (RelativeLayout) findViewById(R.id.filterloc);
            filter.setVisibility(View.INVISIBLE);
            ImageButton filteropen = (ImageButton) findViewById(R.id.aufklaupen);
            filteropen.setVisibility(View.VISIBLE);
            filterclose.setVisibility(View.INVISIBLE);
        }
    });
}



class ReadJSON extends AsyncTask<String,Integer,String> {






    @Override
    protected String doInBackground(String... params) {
        return readURL(params[0]);
    }

    @Override
    protected void onPostExecute(String content) {

        try{
            JSONObject jo = new JSONObject(content);
            JSONArray ja = jo.getJSONArray("contacts");

            for(int i=0;i<ja.length();i++){
                JSONObject po = ja.getJSONObject(i);
                arrayList.add(new productforloc(
                        po.getString("imageurl"),
                        po.getString("name"),
                        po.getString("street"),
                        po.getString("postalcode"),
                        po.getString("musicstyle"),
                        po.getString("musicsecond"),
                        po.getString("entry"),
                        po.getString("opening"),
                        po.getString("agegroup"),
                        po.getString("urlbtn"),
                        po.getString("Fsk"),
                        po.getString("city"),
                        po.getString("bg")

                ));

            }



        } catch (JSONException e) {
            e.printStackTrace();
        }
        final CustomListAdapterforloc adapter = new CustomListAdapterforloc(getApplicationContext(),R.layout.model,arrayList);

        lv.setAdapter(adapter);
        if(pDialog.isShowing()){
            pDialog.dismiss();
        }
        }

    }




private String readURL(String url){
    StringBuilder content = new StringBuilder();


   try{
        URL uri = new URL(url);
        URLConnection urlConnection = uri.openConnection();
        BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String line;
        while((line = bufferedReader.readLine()) !=null){
            content.append(line+"\n");


        }
        bufferedReader.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return content.toString();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    productforloc pForloc = arrayList.get(position);

    Intent intent = new Intent();
    intent.setClass(this,DetailActivity.class);

    intent.putExtra("name",pForloc.getName());
    intent.putExtra("imageurl",pForloc.getImage());
    intent.putExtra("street",pForloc.getStreet());
    intent.putExtra("postalcode",pForloc.getPostalcode());
    intent.putExtra("entry",pForloc.getEntry());
    intent.putExtra("agegroup",pForloc.getAgegroup());
    intent.putExtra("opening",pForloc.getOpening());
    intent.putExtra("urlbtn",pForloc.getUrlbtn());
    intent.putExtra("Fsk",pForloc.getFsk());
    intent.putExtra("city",pForloc.getCity());
    intent.putExtra("musicstyle",pForloc.getMusicstyle());
    intent.putExtra("musicsecond",pForloc.getMusicsecond());
    intent.putExtra("bg",pForloc.getBg());
    startActivity(intent);


}

/**
 * Async task class to get json by making HTTP call

 }



 */

}

这是我的 Customlistadapter Activity;

public class CustomListAdapterforloc extends ArrayAdapter<productforloc>{

ArrayList<productforloc> products;
Context context;
int resource;
public CustomListAdapterforloc(Context context, int resource, List<productforloc> products) {

    super(context, resource, products);
    this.products = (ArrayList<productforloc>) products;
    this.context = context;
    this.resource = resource;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView== null){
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.model,null,true);
    }
    productforloc product = getItem(position);

    ImageView imageView = (ImageView) convertView.findViewById(R.id.imagelist);
    Picasso.with(context).load(product.getImage()).into(imageView);
    TextView txtName= (TextView) convertView.findViewById(R.id.namelist);
    txtName.setText(product.getName());



    return convertView;

}

}

【问题讨论】:

  • 你为什么不把你想要的项目保留在你的产品数组列表中?这不是你的解决方案吗?也可以在这行之前做,final CustomListAdapterforloc adapter = new CustomListAdapterforloc(getApplicationContext(),R.layout.model,arrayList);
  • 是的,当然,这是我想做的,但我该怎么做

标签: android listview filtering


【解决方案1】:

intent获取当前城市名称。

String currentCityName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);

    Intent i = getIntent();
    currentCityName = i.getExtras().getString("cityname");

    ...........
    .................
}

在将productforloc 对象添加到ArrayList 之前,添加condition 以匹配cityNamecurrentCityName

    try {
        JSONObject jo = new JSONObject(content);
        JSONArray ja = jo.getJSONArray("contacts");

        for(int i=0;i<ja.length();i++){
            JSONObject po = ja.getJSONObject(i);

            String cityName = po.getString("city");

            if(!cityName.equals(currentCityName)) {
                arrayList.add(new productforloc(
                    po.getString("imageurl"),
                    po.getString("name"),
                    po.getString("street"),
                    po.getString("postalcode"),
                    po.getString("musicstyle"),
                    po.getString("musicsecond"),
                    po.getString("entry"),
                    po.getString("opening"),
                    po.getString("agegroup"),
                    po.getString("urlbtn"),
                    po.getString("Fsk"),
                    po.getString("city"),
                    po.getString("bg")));
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多