我有主要活动和五个片段。在主要活动中我定义工具栏。在每个片段中,我都有一个包含JSON数据的listview(我使用volley labrary从JSON获取数据)。
1 即可。我想使用微调器过滤数据,即使用微调器我选择特定的" city"并在我的listview中显示只选择listview项目。那么如何实现呢?
2 。所有片段都从一个JSON中获取数据,并且所有片段都显示相似的数据。我希望当一个微调器项从一些片段中选择时它影响到其他片段,即如果在一些片段中选择了" mycity"在其他片段中必须只显示带有" mycity"的listview项目。如何改变呢?
我尝试这样做,但无效。任何想法,帮助对我来说都很重要。对不起英语不好
MainActivity
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
//Checkong internet connect
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
} else {
Toast.makeText(this, "Проверьте соединение с интернотом" , Toast.LENGTH_LONG).show();
}
}
/**
* Adding fragments to ViewPager
* @param viewPager
*/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new MainFragment(),"Все обьявления");
adapter.addFrag(new OneFragment(), "Нужен помощь" );
adapter.addFrag(new TwoFragment(), "Потеряно" );
adapter.addFrag(new ThreeFragment(), "Найдено" );
adapter.addFrag(new FourFragment(), "Отдам даром" );
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
MainFragment
public class MainFragment extends Fragment {
// Log tag
private static final String TAG = "zhaksyadam";
public static String region="Алматы";
// Movies json url
private static final String url = "http://zhaksy-adam.kz/api/Requisitions/GetRequisition";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
Spinner spinner;
public MainFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
listView = (ListView) rootView.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);
//adding spinner
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(true);
LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflator.inflate(R.layout.spinnerwound, null);
spinner = (Spinner) vi.findViewById(R.id.spin);
ArrayAdapter<CharSequence> spinneradapter = ArrayAdapter.createFromResource(getActivity(),
R.array.planets_array, R.layout.spinner_item);
spinner.setAdapter(spinneradapter);
((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(vi);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
region = parent.getItemAtPosition(position).toString();
Log.d(TAG,"MainFragment:"+region);
// Creating volley request obj
final JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
hidePDialog();
if (movieList != null)
movieList.clear();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
String city_json = obj.getString("city");
if (region.equals(city_json)) {
String img = obj.getString("Picture1");
if (img != null) {
img = "http://zhaksy-adam.kz" + img;
img = img.replace("~/", "/");
img = img.replace(" ", "%20");
}
String date = obj.getString("Date");
String[] s = date.split("T");
movie.setTitle(obj.getString("Title"));
movie.setThumbnailUrl(img);
movie.setName(obj.getString("FullName"));
movie.setYear(s[0]);
movie.setRegion(obj.getString("region"));
movie.setCity(obj.getString("city"));
movie.setDescription(obj.getString("Description"));
movie.setMobile(obj.getString("Phone1"));
movie.setMail(obj.getString("E_mail"));
movie.setType(obj.getString("type"));
// adding movie to movies array
movieList.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
AppController.getInstance().addToRequestQueue(movieReq);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// Showing progress dialog before making http request
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.show();
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}