0

I'm new in android programming and in my first app i need to understand how to easily populate a ListView getting data from SQlite. For best understanding, my app has 4 Tabs in the main activity (Tab Layout), switching the tabs i need to load the data previously saved in the database and populate the ListView (using Support Library). I've found some examples using CursorAdapter, like this one https://guides.codepath.com/android/Populating-a-ListView-with-a-CursorAdapter but i don't know how to make work the ViewPagerAdapter with the CursorAdapter.

ViewPagerAdapter

import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.content.Context; public class ViewPagerAdapter extends FragmentStatePagerAdapter { CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created private Context context; // Build a Constructor and assign the passed Values to appropriate values in the class public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) { super(fm); this.Titles = mTitles; this.NumbOfTabs = mNumbOfTabsumb; } //This method return the fragment for the every position in the View Pager @Override public Fragment getItem(int position) { if(position == 0) // if the position is 0 we are returning the First tab { Tab1 tab1 = new Tab1(); return tab1; } else if(position == 1){ Tab2 tab2 = new Tab2(); return tab2; } else if(position == 2) { Tab3 tab3 = new Tab3(); //THIS IS THE TAB TO POPULATE return tab3; //HOW TO RETURN TAB3 (CURSOR ADAPTER)? } else if(position == 3) { Tab4 tab4 = new Tab4(); return tab4; } else { Tab1 tab1 = new Tab1(); return tab1; } } // This method return the titles for the Tabs in the Tab Strip @Override public CharSequence getPageTitle(int position) { return Titles[position]; } // This method return the Number of tabs for the tabs Strip @Override public int getCount() { return NumbOfTabs; } } 

Tab3.java (i've modified this Tab java according to the example linked before)

import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v4.widget.CursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; import com.tyr.R; import com.myapp.library.DatabaseNumbersHandler; public class Tab3 extends CursorAdapter { public Tab3(Context context, Cursor cursor, int flags) { super(context, cursor, 0); } // The newView method is used to inflate a new view and return it, // you don't bind any data to the view at this point. @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return LayoutInflater.from(context).inflate(R.layout.tab_3_list, parent, false); } // The bindView method is used to bind all data to a given view // such as setting the text on a TextView. @Override public void bindView(View view, Context context, Cursor cursor) { // Find fields to populate in inflated template TextView numTitle = (TextView) view.findViewById(R.id.num_title); // Extract properties from cursor String body = cursor.getString(cursor.getColumnIndexOrThrow("title")); // Populate fields with extracted properties numTitle.setText(body); // TodoDatabaseHandler is a SQLiteOpenHelper class connecting to SQLite DatabaseNumbersHandler handler = new DatabaseNumbersHandler(context); // Get access to the underlying writeable database SQLiteDatabase db = handler.getWritableDatabase(); // Query for items from the database and get a cursor back Cursor todoCursor = db.rawQuery("SELECT title FROM NUMBERS", null); // Find ListView to populate ListView lvItems = (ListView) view.findViewById(R.id.tab_3_list); // Setup cursor adapter using cursor from last step Tab3 todoAdapter = new Tab3(context, todoCursor, 0); // Attach cursor adapter to the ListView lvItems.setAdapter(todoAdapter); } } 

tab_3_list.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.tyr.MainSettings"> <ListView android:id="@+id/tab_3_list" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView> </RelativeLayout> 

tab_3.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/num_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="3" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> 

Tab4.java (the other Tabs are like this one, and show a simple Text)

public class Tab4 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.tab_4,container,false); return v; } } 

tab_4.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="You Are In Tab 4" android:id="@+id/textView" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> 

How can i return correctly the tab3 populating the ListView?

1 Answer 1

1

The method getItem returns a fragment so you can't put cursore Adapter there so you have to change tab3 to become a list fragment

Sign up to request clarification or add additional context in comments.

3 Comments

ok but i read that CursorAdapter is the best approach for populate a ListView getting data from db, so it's better if i can modify the ViewPagerAdapter only.
The problem is not how to populate it the problem is how to host the list and since you are using tab layout you have to use list fragment or host tab_3_list.xml inside a fragment and yes you also need to modify the viewPagerAdapter
thank you, with ListFragment it works! Now i can go on with the development

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.