Problem with tablayout and sqlite

Asked

Viewed 129 times

0

Hello guys I’m reformulating an app and includes in the main Activity a tablayout that inflates two Fragments. An Fragment should present on a cardview all data stored in the database. Scrolling the screen to the left the other Fragment should present in another cardview all the items specified in a different query.

It turns out that if I leave Fragment two empty, Fragment 1 presents the data correctly, however if I program Fragment 2 to present the specific data fragment 1 happens to show this data as well.

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

I wonder why that is? How to make each Fragments show different data between them?

  • If Fragment 2 is not empty, the app works correctly?

  • @Rene Freak if I run the app the way it is being shown above the app runs and the cardviews of the two Ragments are filled with the same data. That is, both are filled with data from query2. If you comment the Fragment code two the Fragment 1 cardview is filled correctly with the query 1 data.

1 answer

1


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

But also, 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 take the data from the other cursor. The second presents the corrections.

  • the code ended up being different when pasting it here, but in the app are correct.

  • I saw that you corrected the question. Have you checked if both queries are returning the same values? You tried debugging (debug) line by line to see when deviations occur?

  • In debug the size of the lists is different, this is the goal, however in Activity with the two Fragments does not show the different results, ie in Fragment 1 must show all the data, already in Fragment 2 only one. The fact that they are using the same Adapter influences?

  • The only Adapter I’ve seen that both Ragments use is from the viewpager and it’s correct. From what you say, it seems that you have debugged (debug) parts of the isolated code. The debug I mentioned is with the app running. Putting the breakpoints strategically, you will notice where the app misses.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.