Android使用ViewPagerAdapter中的CursorAdapter填充ListView

时间:2015-11-02 16:56:20

标签: android sqlite listview

我是Android编程的新手,在我的第一个应用程序中,我需要了解如何轻松填充从SQlite获取数据的ListView。为了更好地理解,我的应用程序在主要活动(选项卡布局)中有4个选项卡,切换我需要加载以前保存在数据库中的数据并填充ListView(使用支持库)的选项卡。我发现了一些使用CursorAdapter的例子,比如这个https://guides.codepath.com/android/Populating-a-ListView-with-a-CursorAdapter,但我不知道如何使用CursorAdapter使V​​iewPagerAdapter工作。

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 (我根据之前链接的示例修改了此Tab java)

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 (其他标签就像这一个,并显示一个简单的文字)

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>

如何正确返回填充ListView的tab3?

1 个答案:

答案 0 :(得分:1)

方法getItem返回一个片段,所以你不能把cursore Adapter放在那里,所以你必须改变tab3成为一个列表片段