how to make search function with edittext .
i have four edittext and one button.
while user fill details the data fatch to sqlit and display to activity 2 page.plz help me.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
package com.example.indradeo;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;
public class MainActivity extends Activity implements OnClickListener
{
EditText editRollno, editName, editMarks;
Button btnView;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
editRollno = (EditText) findViewById( R.id.editRollno );
editName = (EditText) findViewById( R.id.editName );
editMarks = (EditText) findViewById( R.id.editMarks );
btnView = (Button) findViewById( R.id.btnView );
btnView.setOnClickListener( this );
db = openOrCreateDatabase( "StudentDB", Context.MODE_PRIVATE, null );
db.execSQL( "CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);" );
}
public void onClick (View view)
{
if (view == btnView) {
if (editRollno.getText().toString().trim().length() == 0) {
showMessage( "Error", "Please enter Emp.ID" );
return;
}
Cursor c = db.rawQuery( "SELECT * FROM student WHERE rollno='" + editRollno.getText() + "'", null );
if (c.moveToFirst()) {
editName.setText( c.getString( 1 ) );
editMarks.setText( c.getString( 2 ) );
} else {
showMessage( "Error", "Invalid Emp.ID" );
clearText();
}
String username = editRollno.getText().toString()+"\n";
String location = editName.getText().toString();
String designation = editMarks.getText().toString();
//dbHandler.insertUserDetails(username,location,designation);
// intent = new Intent(MainActivity.this,DetailsActivity.class);
//startActivity(intent);
//Toast.makeText(getApplicationContext(), "Details fatch Inserted Successfully",Toast.LENGTH_SHORT).show();
}
}
public void showMessage (String title, String message)
{
Builder builder = new Builder( this );
builder.setCancelable( true );
builder.setCancelable( true );
builder.setTitle( title );
builder.setMessage( message );
builder.show();
}
public void clearText ()
{
editRollno.setText( "" );
editName.setText( "" );
editMarks.setText( "" );
editRollno.requestFocus();
}
}
xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myLayout"
android:stretchColumns="0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="147dp"
android:layout_height="33dp"
android:layout_x="130dp"
android:layout_y="206dp"
android:text="@string/title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="7dp"
android:layout_y="310dp"
android:text="@string/roll_no" />
<EditText
android:id="@+id/editRollno"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_x="257dp"
android:layout_y="297dp"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="370dp"
android:text="@string/name" />
<EditText
android:id="@+id/editName"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_x="254dp"
android:layout_y="367dp"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="443dp"
android:text="@string/marks" />
<EditText
android:id="@+id/editMarks"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_x="254dp"
android:layout_y="442dp"
android:inputType="number" />
<Button
android:id="@+id/btnView"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_x="287dp"
android:layout_y="508dp"
android:text="@string/Search" />
AbsoluteLayout>

Laxmidhar SahooPosted May 23, 2020, 1:14 PM
Laxmidhar SahooPosted Jun 12, 2020, 12:29 PM