Я просмотрел много результатов stackoverflow, но результаты не подходят.
необходимо добавить searchView для моего TabLayout (с динамическим (может иметь n вкладок)),
я использовал * map для хранения данных json, используя их размер, я создаю вкладку, которая переходит к одному классу фрагмента, например this .
вот мой код ниже
public class MainActivity extends AppCompatActivity {
List<String> tab_name = new ArrayList<>();
Map<String, ArrayList<Model>> map = new HashMap<>();
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tabs);
viewPager = findViewById(R.id.container);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://channelkonnect.com/ifb/userApi/")
.addConverterFactory(GsonConverterFactory.create())
.build();
final RequestInterface request = retrofit.create(RequestInterface.class);
Call<JsonObject> call = request.getJSON();
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(@NonNull Call<JsonObject> call, @NonNull Response<JsonObject> response) {
JsonObject json_response = response.body();
assert json_response != null;
Gson gson = new Gson();
JSONObject object = null;
try {
object = new JSONObject(gson.toJson(json_response));
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject forecaseobject = null;
try {
assert object != null;
forecaseobject = (JSONObject) object.get("forecast");
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject month;
try {
assert forecaseobject != null;
month = (JSONObject) forecaseobject.get("month");
Iterator iterator = month.keys();
while (iterator.hasNext()) {
// modelList.clear();
String key = iterator.next().toString();
Log.d("Key", key);
tab_name.add(key);
JSONArray jsonArray = month.getJSONArray(key);
ArrayList<Model> modelList = new ArrayList<>();
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject tempObj = jsonArray.getJSONObject(j);
String id = tempObj.getString("prod_name");
String price = tempObj.getString("price");
String product = tempObj.getString("prodId");
String qty = tempObj.getString("Qty");
String date = tempObj.getString("date");
modelList.add(new Model(id, price, product, qty, date));
map.put(key, modelList);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("aspect detail", "outside while: " + map);
for (int i = 0; i < map.size(); i++) {
tabLayout.addTab(tabLayout.newTab().setText(tab_name.get(i)));
}
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
}//on_response
@Override
public void onFailure(@NonNull Call<JsonObject> call, @NonNull Throwable t) {
}//on_failure
});//enqueue
}//on create
class ViewPagerAdapter extends FragmentPagerAdapter {
private int mNumOfTabs;
ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
// guys here i'm sending the arrayList of a particular month
ArrayList<Model> tempmodel = map.get(tab_name.get(position));
return Detailfragment.newInstance(tempmodel);
}
@Override
public int getCount() {
return mNumOfTabs;
}
// @Override
// public CharSequence getPageTitle(int position) {return super.getPageTitle(position); }
}
private interface RequestInterface {
@GET("getForecast?userCode=EMP001&chnlid=2")
Call<JsonObject> getJSON();
}
}
так скажите мне, как реализовать searchView для моего случая ....