Problem with tablayout and sqlite

0

Hello everyone, I'm re-formulating an app and include in the main activity a tablayout that inflates two fragments. A fragment should display in a cardview all the data stored in the database. Rolling the screen to the left the other fragment should display in another cardview all the items specified in a different query.

It turns out that if I leave fragment two empty, fragment 1 displays the data correctly, however if you program fragment 2 to display the specific data, fragment 1 will show that data as well.

In my helper I have two different querys for each situation that I call them within each 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);
    }
}
}

Would you like to know why this happens? How do I make each of the fragments show the distinct data between them?

    
asked by anonymous 15.04.2017 / 22:57

1 answer

1

It may be that, even though they appear to be different, selects has the same result. And therefore, they are equal.

But in addition, in query 2, there are some errors. This excerpt:

if (cursoresp.moveToFirst()){
    do {
        Lista lista2 = new Lista();
        lista2.setSubcategoria(cursor.getString(0));
        lista2.setClassificacao(cursor.getString(1));               
        lista2.setDescricao(cursor.getString(2));                
        lista2ArrayList.add(lista2);
    }while(cursoresp.moveToNext());
}

should be:

   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());
    }

Compare and see the small differences. In the first case, you get the data from the other cursor . The second one presents the fixes.

    
15.04.2017 / 23:45