samedi 13 février 2016

how to show SQLite data in simpleadapter

I am facing an issue. i am trying to show sqlite data in the simple adapter but the data is not showing up in my application i don't know what is the wrong. i am new to android development need help.....

Here is my code

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;


@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);
    arrayList = new ArrayList<HashMap<String,String>>();

    try{

        Cursor c = database.showAllData();


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

        }


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

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


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

    SimpleAdapter adapter = new SimpleAdapter(this, arrayList, R.layout.user_info_layout, s, i);

    listView.setAdapter(adapter);

}
}

And this is my SQLite code

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

layout....

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    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>

Aucun commentaire:

Enregistrer un commentaire