2015-11-02 80 views
0

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

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

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

<?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> 

如何正确返回tab3 popu使用ListView?

回答

1

,所以你不能把cursore适配器有那么你必须改变TAB3成为列表片段

+0

确定,但我读了CursorAdapter的是填充最好的方法的getItem返回一个片段的方法,一个ListView从获取数据db,所以最好是我只能修改ViewPagerAdapter。 – MattC

+0

问题不在于如何填充它问题是如何承载列表,因为你使用标签布局你必须使用列表片段或主机tab_3_list.xml里面的一个片段,是的,你也需要修改viewPagerAdapter –

+0

好的,我尝试使用列表片段,谢谢你的时刻! – MattC