Friday, 9 March 2018

Include List View

Hi All,

To show data in listview from Android there are two steps


Step I 
Create a Layout file with one TextView ,ie label box

<?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:orientation="vertical" >
<TextView
 android:id="@+id/listviewtext"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="TextView" />
</LinearLayout>
Step II
Place one List view in another activity.
Use this code in onCreate() function to view data from DB in list view.



public void HUP1()
                {
                String [] listitem;
                int i=0;
                listitem=new String[3];
                ListView lstview=(ListView)findViewById(R.id.lstview);
                SQLiteDatabase hupdb;
                hupdb=this.openOrCreateDatabase("hup", MODE_PRIVATE, null);
                hupdb.openDatabase("data/data/com.example.snit2/databases/hup", null, SQLiteDatabase.OPEN_READONLY);
                String s="select * from registration";
                Cursor c=hupdb.rawQuery(s, null);
                int count=c.getCount();
                listitem=new String[count];
                String val="";
                    if(c!=null)
                    {
                                   if(c.moveToFirst())
                       {
                         do
                         {
                                 String username=c.getString(0);
                                 String abc=c.getString(1);
                                 val=username + " " + abc;
                                 listitem[i]=val;
                                 i++;
                         }while(c.moveToNext());
                       }
                    }
                   
                    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.layoutview,R.id.txtview,listitem);

lstview.setAdapter(adapter);

lstview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewint positionlong id) {
// TODO Auto-generated method stub
String entry= (String) parent.getAdapter().getItem(position);
//String message = entry.getMessage();
Toast.makeText(getApplicationContext(), entry, Toast.LENGTH_LONG).show();
}
});

}

No comments:

Post a Comment