如何在simpleadapter中显示SQLite数据

时间:2016-02-13 21:18:42

标签: java android sqlite

我正面临一个问题。我试图在简单的适配器中显示sqlite数据,但数据没有显示在我的应用程序中我不知道有什么不对。我是新手Android开发需要帮助.....

这是我的代码

public class user extends AppCompatActivity {

    sqlite_database database;
    ListView listView;
    ArrayList<HashMap<String, String>> arrayList;
    String stname,stfname,contact;
    HashMap<String, String> hmap;
    String n,f,c;
    TextView studentName, studentFatherName,studnetContact;
    Button show;



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

        database = new sqlite_database(getApplicationContext());
        listView=(ListView)findViewById(R.id.userlistView);
        show = (Button)findViewById(R.id.btnShoww);
        studentName = (TextView)findViewById(R.id.studentName);
        studentFatherName = (TextView)findViewById(R.id.studentFName);
        studnetContact = (TextView)findViewById(R.id.studentContact);

        studentName.setText(n);
        studentFatherName.setText(f);
        studnetContact.setText(c);


        ShowData();
        arrayList = new ArrayList<HashMap<String,String>>();
        try{

            Cursor c = database.showAllStudentData();


            while(c.moveToNext())
            {
                hmap= new HashMap<String, String>();
                stname=c.getString(0);
                hmap.put("n", stname);
                stfname=c.getString(1);
                hmap.put("f", stfname);
                contact=c.getString(2);
                hmap.put("c", contact);
                arrayList.add(hmap);

            }


        }
        catch(Exception e){
            Log.e("error",e.getMessage());

        }


        String from[]={n,f,c};


        int to[]={R.id.studentName,R.id.studentFName,R.id.studentContact};

        SimpleAdapter adapter = new SimpleAdapter(this, arrayList, R.layout.user_info_layout, from, to);

        listView.setAdapter(adapter);

    }


    public void ShowData()
    {
        show.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Cursor result = database.showAllStudentData();
                        if (result.getCount() == 0) {
                            //show Message
                            showMessage("Error", "Nothing is here");
                            return;
                        }
                        StringBuffer buffer = new StringBuffer();
                        while (result.moveToNext()) {
                            buffer.append("Name: " + result.getString(0) + "\n");
                            buffer.append("Father Name: " + result.getString(1) + "\n");
                            buffer.append("Contact: " + result.getString(2) + "\n\n");
                        }
                        showMessage("Data", buffer.toString());
                    }
                }
        );
    }
    public void showMessage (String title, String message)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }

}

这是我的SQLite代码

public Cursor showAllData()
    {
        SQLiteDatabase mydatabase = helper.getWritableDatabase();
        Cursor result = mydatabase.rawQuery("select * from "+ Helper.TABLE_NAME,null);
        return result;
    }

...布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userImage" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"></LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/studentName" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/studentFName" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/studentContact" />

</LinearLayout>

userActivity

<?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:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.ks.doit.user">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#006700"
        android:id="@+id/toolbar"></android.support.v7.widget.Toolbar>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userlistView"
        android:layout_below="@+id/toolbar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show All Data"
        android:id="@+id/btnShoww"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

你应该使用CursorAdapter,例如你从数据库中查询数据:

            // columns
            final String[] columns = new String[] { "stname", "stfname, "contact" };
            // resource id's
            final int[] to = new int[] { R.id.studentName, R.id.studentFName, R.id.studentContact };

            // adapter
            final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.student_layout, cursor, columns, to);

            // set adapter to listview
            listView.setListAdapter(mAdapter);

答案 1 :(得分:0)

我已修复它!它创建了一个空字符串列表,名为 -n

所以,应该像这样:

from[]