HOW TO - 从sqlite数据库填充listview片段

时间:2014-04-05 16:25:38

标签: android sqlite fragment android-listfragment

我被困住了,我无法就如何填写listfragment的{​​{1}}记录提出建议。看起来我已经尝试了许多教程,但没有没有坐下或失败。我们做SQLite database的最大问题是因为我知道如何将Listview放入活动中,理论上如此。

在这里加入mainActivity,特定片段和数据库操作类。我们非常欢迎任何帮助。

MainActivity.java

onCreateView

PoznamkaFragment.java

package sk.tomino.login;

import java.util.Locale;

import sk.tomino.login.NastaveniaActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends FragmentActivity {

    public static final String PREZIVKA = "prezivka";

    MyPagerAdapter mMyPagerAdapter;

    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent i = getIntent();
        String prezivka = getResources().getString(R.string.logged)+" "+i.getStringExtra(PREZIVKA);

        if (prezivka.length()>0)
            Toast.makeText (this, prezivka, Toast.LENGTH_SHORT).show();

        mMyPagerAdapter = new MyPagerAdapter(
                getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mMyPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            switch(position){
                 case 0:
                     return UlohaFragment.newInstance();
                 case 1:
                     return PredmetFragment.newInstance();
                 case 2:
                     return PoznamkaFragment.newInstance();
                 case 3:
                     return SviatokFragment.newInstance();
                 default: 
                     return UlohaFragment.newInstance();
            }
        }

        @Override
        public int getCount() {
            // Show 4 total pages.
            return 4;
        }

        @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);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            case 3:
                return getString(R.string.title_section4).toUpperCase(l);
            }
            return null;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_pridat:
                openPridaj();
                return true;
            case R.id.action_hladat:
                openHladaj();
                return true;
            case R.id.action_settings:
                openSettings();
                return true;
            case R.id.action_app:
                openApp();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void openPridaj()
    {
        Intent intent = new Intent(this, PridajActivity.class);
        startActivity(intent);  
    }

    public void openHladaj()
    {
        Intent intent = new Intent(this, HladajActivity.class);
        startActivity(intent);  
    }

    public void openSettings()
    {
        Intent intent = new Intent(this, NastaveniaActivity.class);
        startActivity(intent);  
    }

    public void openApp()
    {
        Intent intent = new Intent(this, AppActivity.class);
        startActivity(intent);
    }
}

DatabaseOp.java

package sk.tomino.login;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

   public static PoznamkaFragment newInstance() {

       PoznamkaFragment f = new PoznamkaFragment();
        return f;
    }
}

1 个答案:

答案 0 :(得分:0)

  • 设置一个包含listView的布局文件。

  • 在正常片段中将此listView充气。

  • 使用CursorAdapter填充列表并将其设置为自定义适配器 列表

为此,请使用常用教程。