Search code examples
androidsqlitelistviewandroid-viewpagercursor

Click on List Item from Sqlite and show related column based on list


I have a Sqlite table like below.

enter image description here

I have made a list with the rawQuery : SELECT HEADER FROM MyData GROUP BY HEADER

 ArrayAdapter arrayAdapter;
 ArrayList<String> listItem = new ArrayList<>();
 final ListView listView = (ListView) view.findViewById(R.id.list);


 SQLiteDatabase sqLiteDatabase = 
 SqliteDatabase.getInstance(this.getContext()).getWritableDatabase();
 Cursor cursor = sqLiteDatabase.rawQuery("SELECT HEADER FROM MyData GROUP BY 
HEADER", new String[]{});

     if (cursor.getCount() == 0) {
         Toast.makeText(getActivity(), "No Data to show", 
Toast.LENGTH_LONG).show();
     } else {
         while (cursor.moveToNext()) {
             listItem.add(cursor.getString(0));
         }
         arrayAdapter = new ArrayAdapter<>(getActivity(), 
android.R.layout.simple_list_item_1, listItem);
         listView.setAdapter(arrayAdapter);

List shows as below:

Letter Number Word

My problem is when I click on list item Suppose(Letter), SwipeViews shows A,

when click on Number SwipeViews shows B and

when click on Word SwipeViews shows C

I want to get, when I will click on Letter, SwipeViews will show only A,B,C

when I will click on Number, SwipeViews will show only 1,2,3

when I will click on Word, SwipeViews will show only Apple, Orange and Banana

This is OnClickListener

listView.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> adapterView, View view, int i, 
   long l) {
         Intent intent=new Intent(getActivity(), SwipeNav.class);
         intent.putExtra(ID_EXTRA, String.valueOf(l));
         startActivity(intent);
     }
     });

This is from Pager Adapter

public class MyPagerAdapter extends FragmentStatePagerAdapter {
private Context context;
public MyPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context= context;}
@Override
public Fragment getItem(int i) {
    ArrayList<String> listItem = new ArrayList<String>();
    SQLiteDatabase sqLiteDatabase = 
SqliteDatabase.getInstance(context).getWritableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery("SELECT NAME FROM MyData", new 
String[]{});
    if (cursor.getCount() == 0) {
    } else {
        while (cursor.moveToNext()) {
            listItem.add(cursor.getString(0));
        }
    }
    Object[] mStringArray = listItem.toArray();
    Fragment fragment = new AFragment();
    Bundle args = new Bundle();
    args.putString(AFragment.ARG_OBJECT, (String)mStringArray[i]);
    fragment.setArguments(args);
    return fragment;
}

In the SwipeNav Activity, I used below to get value

if (getIntent().hasExtra(ListNavAdapter.ID_EXTRA)){
     String myInt = getIntent().getStringExtra(ListNavAdapter.ID_EXTRA);

     listSwipePagerAdapter = new ListSwipePagerAdapter(getSupportFragmentManager(),this);
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(listSwipePagerAdapter);

Solution

  • Your issue appears to be that you are using l, expecting it to be the id. Unless a CursorAdapter is used it will be the position (the same as the 3rd parameter passed except long).

    • that is if you click on Letter you will pass 0 via the intent, click on Number and it will pass 1 and if you click on Word it will pass 2.

    What you want to pass is the value of the clicked view (Letter Number or Word) and then use that to SELECT the respective rows.

    • You will never be able to reliably get an id as no such value is available in the ArrayList.

    Therefore instead of intent.putExtra(ID_EXTRA, String.valueOf(l)); you want

    intent.putExtra(ID_EXTRA, ((String)adapterView.getItem(i)));
    

    or alternately

    intent.putExtra(ID_EXTRA,((TextView) view).getText().toString());
    

    Noting that the value from the list i.e the distinct values of the Header column (Letter, Number or Word according to the sample data).

    To then get the values you want you would need a query, in the SwipeNav activity, such as

    SELECT NAME FROM MyData WHERE HEADER = 'the_value_from_ID_EXTRA'

    • where the_value_from_ID_EXTRA would be replaced with the value extracted from the intent extras with the key ID_EXTRA that was passed to the SwipeNav activity.

    • Note the above code is in-principle code, it has not been tested or run and may therefore have some errors.