I am new to android development. I am trying to get the first Activity (Activity2.java) with 3 editexts that demand details in the form of (_ID, STATE , FARMER_NAME) & it contains three buttons , one to insert records, one to delete, & one that helps in viewing records of the database in next activity that is my MainActivity(actually i have made my 2nd activity page in the name of MAinActivity.java). I want those database records to be shown in List Format with customised layout of each row. But whenever i click on "View Record" button my app crashes. I have placed an intent also.
Here's the code:
for Actvity2.java
import android.app.Activity;
import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by NILESH on 6/20/2015.
*/
public class Activity2 extends Activity implements View.OnClickListener {
EditText e1,e2,e3,e4;
Button b1, b2, b3;
DatabaseHandler db = new DatabaseHandler(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.edittext1);
e2=(EditText)findViewById(R.id.edittext2);
e3=(EditText)findViewById(R.id.edittext4);
e4=(EditText)findViewById(R.id.edittext3);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
public void onClick(View v) {
if (v.equals(b1)) {
Toast.makeText(this, "button1 selected",
Toast.LENGTH_SHORT).show();
db.insertRecord(e1.getText().toString(),e2.getText().toString(),e3.getText().toString());
}
if (v.equals(b2)) {
Toast.makeText(this,"button2 selected",Toast.LENGTH_SHORT).show();
db.deleteRecord(e4.getText().toString());
}
if (v.equals(b3)) {
Toast.makeText(this,"button3 selected",Toast.LENGTH_SHORT).show();
Intent i=new Intent(this,MainActivity.class);
int count = db.getcount();
String s = String.valueOf(count);
i.putExtra("a",s);
startActivity(i);
}
}
}
For MainActivity.java
import android.app.ListActivity;
import java.util.ArrayList;
import java.util.ArrayList;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends ListActivity {
DatabaseHandler db=new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Toast.makeText(this,getIntent().getExtras().getString("a"),Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
int h=db.getcount();
Toast.makeText(this,"kkk"+h,Toast.LENGTH_SHORT).show();
Cursor b=db.getAllData();
String s="";
startManagingCursor(b);
if(b.moveToFirst()) {
s = s + b.getString(2);
}
Toast.makeText(this,"kkk"+s,Toast.LENGTH_SHORT).show();
String[] from=new String[]{"_ID","State","Farmer_name"};
int[] to=new int[]{R.id.t1,R.id.t2,R.id.t3};
SimpleCursorAdapter sca=new SimpleCursorAdapter(this,R.layout.rows,b,from,to);
setListAdapter(sca);
}
@Override
protected void onListItemClick(ListView l,View v,int position,long id)
{
int item=position;
Toast.makeText(this,item+" selected",Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
*****DatabaseHandler.java*****
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
public DatabaseHandler(Context context) {
super(context, "DbTest2", null, 1);
}
@Override
public void onCreate (SQLiteDatabase db){
String sql = "create table list(_ID TEXT,State TEXT,Farmer_Name TEXT)";
db.execSQL(sql);
}
@Override
public void onUpgrade (SQLiteDatabase db,int oldVersion, int newVersion){
String sql = "DROP TABLE list";
db.execSQL(sql);
onCreate(db);
}
public void insertRecord(String var1, String var2,String var5) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "insert into list values('"+var1+"','"+var2+"','"+var5+"')";
db.execSQL(sql);
}
public void deleteRecord(String var3) {
// String var4="'"+var3+"'";
SQLiteDatabase db = this.getWritableDatabase();
//use var4 to store var3 as var4='var3' by concatenation
String sql = "delete from list where _ID='"+var3+"'";
db.execSQL(sql);
}
public int getcount() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + "list", null);
int x = c.getCount();
c.close();
return x;
}
public Cursor getAllData(){
SQLiteDatabase db=this.getReadableDatabase();
Cursor c=db.rawQuery("SELECT * FROM " + "list", null);
return c;
}
}
Aucun commentaire:
Enregistrer un commentaire