Olá pessoal estou reformulando um app e inclui na activity principal um tablayout que infla dois fragments. Um fragment deveria apresentar num cardview todos os dados armazenados no banco de dados. Rolando a tela para a esquerda o outro fragment deveria apresentar em outro cardview todos os itens especificados numa query diferente.
Acontece que se eu deixar o fragment dois vazio, o fragment 1 apresenta os dados corretamente, no entanto se programar o fragment 2 para apresentar os dados específicos o fragmente 1 passa a mostrar esses dados também.
No meu helper tenho duas querys diferentes para cada situação que chamo elas dentro de cada fragment.
HELPER
QUERY 01
/* Recupera todos dados do banco de dados*/
public ArrayList<Lista> getLista1(){
ArrayList<Lista> listaArrayList = new ArrayList<>();
String query = "SELECT * FROM "+AssetHelper.TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()){
do {
Lista lista1 = new Lista();
lista1.setSubcategoria(cursor.getString(0));
lista1.setClassificacao(cursor.getString(1));
lista1.setDescricao(cursor.getString(2));
listaArrayList.add(lista1);
}while(cursor.moveToNext());
}
return listaArrayList;
}
QUERY 2
/* Recupera os dados específicos do banco de dados*/
public ArrayList<Lista> getLista2(){
ArrayList<Lista> lista2ArrayList = new ArrayList<>();
String query2 = "SELECT * FROM "+AssetHelper.TABLE_NAME+" WHERE "+AssetHelper.COLUM_ESP+" = 1";
SQLiteDatabase dbesp = this.getReadableDatabase();
Cursor cursoresp = dbesp.rawQuery(query2,null);
if (cursoresp.moveToFirst()){
do {
Lista lista2 = new Lista();
lista2.setSubcategoria(cursoresp.getString(0));
lista2.setClassificacao(cursoresp.getString(1));
lista2.setDescricao(cursoresp.getString(2));
lista2ArrayList.add(lista2);
}while(cursoresp.moveToNext());
}
return lista2ArrayList;
}
ACTIVITY
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Fragments
viewPager = (ViewPager) findViewById(R.id.viewpager);
addFragmentsToViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
//Fim fragments
}
private void addFragmentsToViewPager(ViewPager viewPager) {
TabsFragmentPagerAdapter adapter = new TabsFragmentPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Lista01Fragment(), getString(R.string.lista_01));
adapter.addFragment(new Lista02Fragment(), getString(R.string.lista_02));
viewPager.setAdapter(adapter);
}
}
FRAGMENT 01
public class Lista01Fragment extends Fragment {
CidsAssetHelper helpher;
List<Lista> lista;
RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public Lista01Fragment {
}
public static Lista01Fragment() newInstance() {
Lista01Fragment() fragment = new Lista01Fragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_lista_01, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
helpher = new AssetHelper(getActivity());
lista = new ArrayList<Lista>();
lista = helpher.getLista1();
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.recyclerview_lista1);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new Adapter(getActivity(),lista);
mRecyclerView.setAdapter(mAdapter);
}
}
FRAGMENT 2
public class Lista02Fragment() extends Fragment {
AssetHelper helpher2;
List<Lista> list2;
RecyclerView mRecyclerView2;
private RecyclerView.Adapter mAdapter2;
private RecyclerView.LayoutManager mLayoutManager2;
public Lista02Fragment() {
// Required empty public constructor
}
public static Lista02Fragment() newInstance() {
Lista02Fragment() fragment = new Lista02Fragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_lista_2, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
helpher2 = new AssetHelper(getActivity());
lista2 = new ArrayList<Lista>();
lista2 = helpher2.getLista2();
mRecyclerView2 = (RecyclerView)getActivity().findViewById(R.id.recyclerview_lista2);
mRecyclerView2.setHasFixedSize(true);
mLayoutManager2 = new LinearLayoutManager(getContext());
mRecyclerView2.setLayoutManager(mLayoutManager2);
mAdapter2 = new Adapter(getActivity(),lista2);
mRecyclerView2.setAdapter(mAdapter2);
}
}
ADAPTER
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
static List<Lista> list;
static Context context;
public Adapter(Context context, List<Lista> list){
this.list = new ArrayList<Lista>();
this.context = context;
this.list = list;
}
@Override
public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.lista, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(Adapter.ViewHolder holder, int position) {
holder.subactegoria.setText(cidsList.get(position).getSubcategoria());
holder.descrabrev.setText(cidsList.get(position).getDescrabrev());
}
@Override
public int getItemCount() {
return list.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView subactegoria,descrabrev;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
subactegoria = (TextView) itemLayoutView.findViewById(R.id.tv_subcategoria);
descrabrev = (TextView)itemLayoutView.findViewById(R.id.tv_descriabreviada);
itemLayoutView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(context,DetalhesActivity.class);
Bundle extras = new Bundle();
extras.putInt("position",getAdapterPosition());
intent.putExtras(extras);
context.startActivity(intent);
}
}
}
Gostaria de saber porque isso acontece? Como fazer para que cada um dos fragments mostre os dados distintos entre eles?