活動類型是「固定標籤+刷卡」。我需要從我的Listvew中的數據庫sqlite中讀取數據,它位於片段xml文件中(不在主要xml中)。 l使用我的舊代碼的一些程序,沒有用於創建列表視圖的方法:不能讀取列表視圖中的sqlite數據庫android
public void createList(){
db = new DB(this);
db.open();
String[] from = new String[] {DB.COLUMN_NAME, DB.COLUMN_TIME };
int[] to = new int[] { R.id.tvTextName, R.id.tvText_time};
scAdapter = new SimpleCursorAdapter(this, R.layout.item, null, from, to, 0);
lvData = (ListView) findViewById(R.id.lvData);
lvData.setAdapter(scAdapter);
getSupportLoaderManager().initLoader(0, null, this);
}
在簡單的活動代碼工作不錯,但我不知道如何使用的代碼片段活動。我嘗試在主類中調用該方法,但該代碼:
lvData = (ListView) findViewById(R.id.lvData);
lvData.setAdapter(scAdapter);
我放入片段類。程序工作,但列表視圖是空的。數據庫不是空的。有代碼的主要部分:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener, LoaderCallbacks<Cursor> {
//*****ОСНОВНЫЕ ПЕРЕМЕННЫЕ*****
private static String TAG = "MainActivity";
private static final int CM_DELETE_ID = 1;
private static final int RESET_STOPWATCH = 2;
static ListView lvData;
static DB db;
static SimpleCursorAdapter scAdapter;
static Context ctx;
static String[] from = new String[] {DB.COLUMN_NAME, DB.COLUMN_TIME };
static int[] to = new int[] { R.id.tvTextName, R.id.tvText_time};
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
ctx = this;
createList();
}
public void createList(){
db = new DB(this);
db.open();
scAdapter = new SimpleCursorAdapter(this, R.layout.item, null, from, to, 0);Log.d(TAG, "1");
getSupportLoaderManager().initLoader(0, null, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
if(position == 0){
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
//getSupportLoaderManager().initLoader(0, null, this);
return fragment;
}
if (position == 1)
{
Fragment fragment2 = new DummySectionFragment2();
Bundle args = new Bundle();
args.putInt(DummySectionFragment2.ARG_SECTION_NUMBER, position + 2);
fragment2.setArguments(args);
return fragment2;
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
public static class DummySectionFragment extends Fragment{
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
static View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_tracker,
container, false);
v = inflater.inflate(R.layout.activity_tracker, container, false);
lvData = (ListView) v.findViewById(R.id.lvData);Log.d(TAG, "2");
lvData.setAdapter(scAdapter);Log.d(TAG, "3");
return rootView;
}
}
public static class DummySectionFragment2 extends Fragment {
//code page 2
}
}
//Класс по управлению БД
static class MyCursorLoader extends CursorLoader {
DB db;
public MyCursorLoader(Context context, DB db) {
super(context);
this.db = db;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = db.getAllData();
return cursor;
}
}
}
}
請!有人可以幫我 – kolodach