Hi all,
To show data in listview from Android there are two steps
1. Create a Layout file with one TextView ,ie label box
2. Place one Button (For insert into db) and One ListView (To show data). After that reuse this code.
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.
package com.example.hupdb1;
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.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
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;
listitem=new String[5];
ListView lstview=(ListView)findViewById(R.id.lstViewhup);
SQLiteDatabase hupdb;
hupdb=this.openOrCreateDatabase("hup", MODE_PRIVATE, null);
hupdb.openDatabase("data/data/com.example.hupdb1/databases/hup", null, SQLiteDatabase.OPEN_READONLY);
String s="select * from registration";
Cursor c=hupdb.rawQuery(s, null);
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());
}
}
//Toast.makeText(this, ""+listitem[4], Toast.LENGTH_LONG).show();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.hupview,R.id.listviewtext,listitem);
lstview.setAdapter(adapter);
lstview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long 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();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HUP1();
}
public void HUP(View hupview)
{ String [] listitem;
SQLiteDatabase hupdb;
hupdb=this.openOrCreateDatabase("hup", MODE_PRIVATE, null);
hupdb.execSQL("CREATE TABLE IF NOT EXISTS REGISTRATION(username text,password text)");
hupdb.execSQL("insert into registration values('HUPA','HUPATAFTKTU')");
Toast.makeText(this, "Data Inserted HUP", Toast.LENGTH_LONG).show();
hupdb.close();
}
@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