The following code gets two strings from a text box with id entername and entername1. The string entered is queried using where clause to match in database and display in list view. The following code works fine if i dont use a where cause and instead use "select * from text". Unable to get the output when i use where clause as shown below. EXAMPLE OUT PUT: Test1: Database already contains the entries abc and xyz. When i enter abc and xyz in respective text boxes and on pressing 'View' i get no output.
package com.example.list;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
SQLiteDatabase mydb;
TextView name,email;
Button save,myview;
ListView lv;
ArrayList data,data1;
EditText entername,entername1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(TextView)findViewById(R.id.entername);
email=(TextView)findViewById(R.id.entername1);
save=(Button) findViewById(R.id.bsave);
//=====CODE FOR SHOWING THE DATA IN TOAST =======
lv=(ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Toast.makeText(getApplicationContext(), data.get(arg2).toString(),3000).show();
}
});
mydb = MainActivity.this.openOrCreateDatabase("androiditu", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT,email varchar,name varchar);");
//=== SAVE BUTTON FOR INSERTING DATA TO DATABASE =======
save.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
mydb.execSQL("insert into test (name) values(?);",new String[] {name.getText().toString()});
mydb.execSQL("insert into test (email) values(?);",new String[] {email.getText().toString()});
Toast.makeText(getApplicationContext(), "DATA INSERTED", 3000).show();
}
});
myview=(Button)findViewById(R.id.bview);
data=new ArrayList();
data1=new ArrayList();
//=VIEW BUTTON TO SHOW DATA INSERTED IN THE LISTVIEW=======
myview.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Loading", 3000).show();
entername=(EditText)findViewById(R.id.entername);
entername1=(EditText)findViewById(R.id.entername1);
String userName=entername.getText().toString();
String userName1=entername1.getText().toString();
Cursor cursor2=mydb.rawQuery("SELECT name,email FROM test where email='"+userName1+"' and name='"+userName+"'" , null);
if (cursor2.moveToFirst())
{
Toast.makeText(getApplicationContext(), "Name’s are:", 3000).show();
data.clear();
do
{
data.add(cursor2.getString(cursor2.getColumnIndex("email")));
data.add(cursor2.getString(cursor2.getColumnIndex("name")));
}
while (cursor2.moveToNext());
//====CODE FOR SHOWING DATA AS A SIMPLE LIST ITEM=========================================
ArrayAdapter <String> adapter=new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1,data);
lv.setAdapter(adapter);
}
else
{
Toast.makeText(getApplicationContext(), "DATA NOT AVAILABLE", 3000).show();
}
cursor2.close();
}
});
}
}