无法在TabLayout中运行Fragment,以显示数据库中的ListView

时间:2019-01-21 03:35:15

标签: android-studio

我尝试使用片段显示列表视图,该列表视图将从数据库中获取列表,并且该片段将显示在选项卡布局中的一个选项卡中。

片段xml文件= R.layout.fragment_frag_feedback

用于数据输入的SQLiteOpenHelper = databaseHelper_feedback

我试图在onCreate()中实现代码,但仍在探索生命周期问题。

这是MainActivity.java

package com.example.user.menuanalysisapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new     ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new Frag_home(), "Home");
        adapter.addFragment(new Frag_feedback(), "Feedback");
        adapter.addFragment(new Frag_announcement(), "Announcement");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>    ();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

这是Frag_feedback.java

package com.example.user.menuanalysisapplication;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class Frag_feedback extends Fragment {
    private SQLiteDatabase db;
    private Cursor cursor;

    public Frag_feedback() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_frag_feedback,container,false);
        ListView listfeedback = (ListView)view.findViewById(R.id.list_feedback);

        SQLiteOpenHelper databaseHelper_feedback = new DatabaseHelper_feedback(view.getContext());
        try{
            db = databaseHelper_feedback.getReadableDatabase();
            cursor = db.query("FEEDBACK",
                new  String[]{"_id","TITLE"},null,null,null,null,null);

            SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_list_item_1,cursor,
                new String[]{"NAME"},new int[]{android.R.id.text1},0);

            listfeedback.setAdapter(listAdapter);

        }catch (SQLiteException e){
            Toast toast = Toast.makeText(view.getContext(),"Database unavailable",Toast.LENGTH_LONG);
            toast.show();
        }
        return  view;
    }
}

当我运行该应用程序时,它根本无法启动。

0 个答案:

没有答案