繁体   English   中英

如何在simpleadapter中显示SQLite数据

[英]how to show SQLite data in simpleadapter

我面临一个问题。 我试图在简单的适配器中显示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>

您应该使用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);

我已经解决了! 它创建了一个名为from[]的空字符串列表。

因此,应该是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM