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 but, like this one http://ift.tt/1MCtcZ1 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://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4" 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>
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://ift.tt/nIICcg"
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?
Aucun commentaire:
Enregistrer un commentaire