Wednesday, 17 January 2018

Showing Data in List view using Android

Hi all,
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);




}


}

Monday, 8 January 2018

SQLite DB

Hi all,

public void hup(View abc) {
 
    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('HUP','TAFTKTU')");
    Toast.makeText(this, "Data Inserted HUP", Toast.LENGTH_LONG).show();
    hupdb.close();*/
    hupdb.openDatabase("data/data/com.example.snit/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(1);
                    val=val+username;
                   
            }while(c.moveToNext());
        }
    }
    Toast.makeText(this, val, Toast.LENGTH_LONG).show();
   
}

Thursday, 4 January 2018

How to load website in activity

Hi All,

See the code below to load a website

public void HUP(View v)
    {
        Intent intent=new Intent(Intent.ACTION_VIEW); 
        intent.setData(Uri.parse("http://www.javatpoint.com")); 
        startActivity(intent); 
    }

Receive Activity

Hi All ,

In the second activity, modify the onCreate() function

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Intent in=getIntent();
        String msg=in.getStringExtra("msg");
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

Send Activity

Hi All,

See the code to send activity.

Do these steps beforehand
1. Create a new class by right clicking the package in src folder
2. Copy paste the code of first activity and change class name.
3. Create a new xml file by right clicking the layout folder under res folder
4. In the First activity add a function HUP() by adding onclick handler in the corresponding layout file

public void HUP(View v)
    {
        EditText msg=(EditText)findViewById(R.id.txtName);
        String txt=msg.getText().toString();
        Intent in=new Intent(MainActivity.this,Activity2.class);
        in.putExtra("msg", txt);
        startActivity(in);
    
    }