如何仅显示列表视图项,其值等于Json中的值

时间:2017-05-02 19:35:17

标签: android listview filtering

我创建了一个应该在Listview中显示景点的应用程序。 数据从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活动;

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;

}

}

1 个答案:

答案 0 :(得分:0)

gymMember获取当前城市名称。

    for (gymMember gymMember : gymMembers.values()) {//where your Object would be gymMember
          if (gymMember.getArea() == x) {
            aMember.add(gymMember);
        }
    }

在将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"); ........... ................. } 之前,添加condition以使cityNamecurrentCityName匹配:

productforloc