Hi all,
Use this for List View in Android.
Step1 : Create a layout file and place a TextView (This is for a single item in Listview)
Step2 : Create a new Layout file and Java file. Place the List view in the Layout file and use the code below under section 2 for loading data into listview.
The first layout file is connected with the Listview in the same code.
Use this for List View in Android.
Step1 : Create a layout file and place a TextView (This is for a single item in Listview)
Step2 : Create a new Layout file and Java file. Place the List view in the Layout file and use the code below under section 2 for loading data into listview.
The first layout file is connected with the Listview in the same code.
Hi all,
To show data in listview from Android there are two steps
1. Create a Layout file with one TextView ,ie label box
To show data in listview from Android there are two steps
1. 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>
-------------------------------------------------------------------------------------------------------------------------
2. Place one Button (For insert into db) and One ListView (To show data). After that reuse this code.
2. Place one Button (For insert into db) and One ListView (To show data). After that reuse this code.
package com.example.hupnew;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
public void HUP1()
{ String [] listitem;
int i=0;
ListView lstview=(ListView)findViewById(R.id.huplistview);
SQLiteDatabase hupdb;
hupdb=this.openOrCreateDatabase("imspect", MODE_PRIVATE, null);
hupdb.openDatabase("data/data/com.example.hupnew/databases/imspect", null, SQLiteDatabase.OPEN_READONLY);
String s="select * from STUD";
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;
//Toast.makeText(this, val, Toast.LENGTH_LONG).show();
listitem[i]=val;
i++;
}while(c.moveToNext());
}
}
//Toast.makeText(this, ""+listitem[4], Toast.LENGTH_LONG).show();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.hupviewlayout,R.id.huptext,listitem);
lstview.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase hupdb;
hupdb=this.openOrCreateDatabase("imspect", MODE_PRIVATE, null);
hupdb.execSQL("CREATE TABLE IF NOT EXISTS STUD(name text,roll text,marks text)");
hupdb.execSQL("insert into STUD values('HUP','108','100')");
hupdb.execSQL("insert into STUD values('HUP','99','100')");
Toast.makeText(this, "Successfully inserted", Toast.LENGTH_LONG).show();
hupdb.close();
HUP1();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
public void HUP1()
{ String [] listitem;
int i=0;
ListView lstview=(ListView)findViewById(R.id.huplistview);
SQLiteDatabase hupdb;
hupdb=this.openOrCreateDatabase("imspect", MODE_PRIVATE, null);
hupdb.openDatabase("data/data/com.example.hupnew/databases/imspect", null, SQLiteDatabase.OPEN_READONLY);
String s="select * from STUD";
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;
//Toast.makeText(this, val, Toast.LENGTH_LONG).show();
listitem[i]=val;
i++;
}while(c.moveToNext());
}
}
//Toast.makeText(this, ""+listitem[4], Toast.LENGTH_LONG).show();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.hupviewlayout,R.id.huptext,listitem);
lstview.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase hupdb;
hupdb=this.openOrCreateDatabase("imspect", MODE_PRIVATE, null);
hupdb.execSQL("CREATE TABLE IF NOT EXISTS STUD(name text,roll text,marks text)");
hupdb.execSQL("insert into STUD values('HUP','108','100')");
hupdb.execSQL("insert into STUD values('HUP','99','100')");
Toast.makeText(this, "Successfully inserted", Toast.LENGTH_LONG).show();
hupdb.close();
HUP1();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
No comments:
Post a Comment