从sqlite填充listView片段

时间:2014-08-09 00:35:29

标签: android sqlite listview fragment android-listfragment

我基本上创建了ToDo列表。我正在处理数据库部分,因此用户可以键入项目,并在单击添加按钮时填充列表。

遗憾的是,我完全迷失了如何让listview填充存储的项目。现在,当我输入内容并单击添加时,会弹出一个Toast消息,其中包含我刚刚添加的项目。现在我不得不让它出现。我已经创建了一个DataHelper.java类来处理sqlite的东西。

这是目前的样子 http://imgur.com/7ztCQSJ

这是我的FragmentTask.java文件

import android.content.Context;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 * 
 */
public class FragmentTasks extends ListFragment implements OnClickListener{

    public FragmentTasks() {
        // Required empty public constructor
    }
    Button button;
    EditText task;
    DataHandler handler;
    //ListView list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        final View view = inflater.inflate(R.layout.fragment_tasks, container, false);
        Button button = (Button)view.findViewById(R.id.button1);
        button.setOnClickListener(this);
        task = (EditText)view.findViewById(R.id.editText1);

        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                String getTasks;
                getTasks = "";
                handler = new DataHandler(getActivity());
                handler.open();
                Cursor C = handler.returnData();
                if(C.moveToFirst()){

                    do
                    {
                        getTasks = C.getString(0);

                    } while(C.moveToNext());
                }

                handler.close();
                Toast.makeText(getActivity(), "Task :" +getTasks, Toast.LENGTH_LONG).show();


            }


        });
        //return inflater.inflate(R.layout.fragment_tasks, container, false);
        /* button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Activity activity = getActivity();

                if (activity != null) {
                    Toast.makeText(activity, R.string.toast_you_just_clicked_a_fragment, Toast.LENGTH_LONG).show();
                }
            }

        });


        */



        return view;

    }

    /* @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        //ArrayAdapter adapter=ArrayAdapter.

                //createFromResource(getActivity(), R.array.tasks, android.R.layout.simple_list_item_1);
        //setListAdapter(getTasks);
    } */

    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
        Toast.makeText(getActivity(), "Item "+i,Toast.LENGTH_SHORT).show();

    }

     @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String getTasks = task.getText().toString();
        handler = new DataHandler(getActivity());
        handler.open();
        long id = handler.insertData(getTasks);
        Toast.makeText(getActivity(), "Data inserted", Toast.LENGTH_LONG).show();
        handler.close();

    //  ArrayAdapter<?> adapter=ArrayAdapter.createFromResource(getActivity(), R.array.tasks, android.R.layout.simple_list_item_1);
    //  setListAdapter(adapter);




/*  button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            String getTasks;
            getTasks = "";
            handler = new DataHandler(getActivity());
            handler.open();
            Cursor C = handler.returnData();
            if(C.moveToFirst()){

                do
                {
                    getTasks = C.getString(0);

                } while(C.moveToNext());
            }

            handler.close();
            Toast.makeText(getActivity(), "Task :" +getTasks, Toast.LENGTH_LONG).show();
        }


    }); */

}}

这是我的DataHandler.java

package com.example.navbar;
import java.sql.SQLException;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DataHandler {
    public static final String TASKS = "tasks";
    public static final String TABLE_NAME = "mytable";
    public static final String DATA_BASE_NAME = "mydatabase";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_CREATE = "create table mytable(tasks text not null);";

    DataBaseHelper dbhelper;
    Context ctx;
    SQLiteDatabase db;

    public DataHandler(Context ctx){
        this.ctx =ctx;
        dbhelper = new DataBaseHelper(ctx);

    }

    private static class DataBaseHelper extends SQLiteOpenHelper{

        public DataBaseHelper(Context ctx){
            super(ctx,DATA_BASE_NAME, null, DATABASE_VERSION);

        }


        @Override
        public void onCreate(SQLiteDatabase db){

            db.execSQL(TABLE_CREATE);
        }

        @Override
        public void onUpgrade (SQLiteDatabase db, int oldversion, int newVersion){

            db.execSQL("DROP TABLE IF EXISTS mytable");
            onCreate(db);

        }
    }

    public DataHandler open(){

        db = dbhelper.getWritableDatabase();
        return this;
    }

    public void close(){
        dbhelper.close();

    }

    public long insertData(String tasks){

        ContentValues content = new ContentValues();
        content.put(TASKS, tasks);
        return db.insertOrThrow(TABLE_NAME, null , content);

    }


    public Cursor returnData(){

        return db.query(TABLE_NAME, new String[] {TASKS}, null, null, null, null, null);
    }

}

fragment_task.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:gravity="center"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="352dp"
        android:divider="#0000CC"
        android:dividerHeight="0.1dp" >

    </ListView>

</LinearLayout>

我认为你们不需要看到这个,但以下是 MainActivity.java

package com.example.navbar;

import com.parse.Parse;
import com.parse.ParseAnalytics;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
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.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    ViewPager viewPager;
    ActionBar actionBar;
    @Override


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


        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                actionBar.setSelectedNavigationItem(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1 = actionBar.newTab();
        tab1.setText("Tab1");
        tab1.setTabListener(this);

        ActionBar.Tab tab2 = actionBar.newTab();
        tab2.setText("Tab2");
        tab2.setTabListener(this);

        ActionBar.Tab tab3 = actionBar.newTab();
        tab3.setText("Tab3");
        tab3.setTabListener(this);

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);



    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        viewPager.setCurrentItem(tab.getPosition());
    }
    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }   

    class MyAdapter extends FragmentStatePagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int i) {
            // TODO Auto-generated method stub

            Fragment fragment = null;

            if (i == 0){

                fragment=new FragmentToday();
            }

            if (i == 1){

                fragment = new FragmentTasks();
            }

            if (i == 2){

                fragment = new FragmentCalendar();
            }   
            return fragment;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 3;
        }









    }

}

0 个答案:

没有答案