hi i am developing an android application on platform 2.1
im presently working on two classes:
Dbhelper.java
RssActivity.java

I have made a students table, its schema is created but my problem is values are not being inserted into it.
There are two textbox: one for Name and other for entering roll no.
Currently my app is working only if i enter roll no as 00113202709 or 12013202709.
I want the values should be taken from the 2 textboxes editText1 and editText2 and inserted in my database.

Guys please help im stuck in this part.

Here are my two classes
Dbhelper.java

package rss.feed;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class Dbhelper extends SQLiteOpenHelper {
    private static final String TAG = Dbhelper.class.getSimpleName();
    public static final String db_name = "member.db";
    public static final int db_version = 1;
    public final String TABLE = "students";
    public final String C_ID = BaseColumns._ID; 
    public  final String C_Name = "SName";
    public  final String C_ENo = "Enroll No";
    Context context;

    public Dbhelper(Context context) {
        super(context, db_name, null, db_version);
        this.context=context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String sql = String.format(
                "create table %s (%s TEXT primary key, %s TEXT, %s TEXT )",
                TABLE, C_ID, C_Name, C_ENo);

        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("drop table if exists " + TABLE);
        this.onCreate(db);

    }

}

RssActivity.java

package rss.feed;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RssActivity extends Activity implements OnClickListener {

    TextView Title;
    EditText Name, ENo;
    Button but;
    public String n1 = "001";
    public String n2 = "120";
    public String s1, s2, s3;
    Dbhelper obj;
    SQLiteDatabase db;
    ContentValues val;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Title = (TextView) findViewById(R.id.textView1);
        Name = (EditText) findViewById(R.id.editText2);
        ENo = (EditText) findViewById(R.id.editText1);
        but = (Button) findViewById(R.id.button1);
        but.setOnClickListener(this);
        obj=new Dbhelper(RssActivity.this);
        db=obj.getWritableDatabase();
    }

    public void onClick(View v) {
        String name = Name.getText().toString();
        String enr = ENo.getText().toString();

        try {

            if (name.equals("")) {
                Name.setError(getText(R.string.Empty));
                Toast.makeText(this, R.string.Empty, Toast.LENGTH_LONG).show();

            } else if ((ENo.getText().toString().length() == 0)) {
                ENo.setError(getText(R.string.Empty));
                Toast.makeText(this, R.string.Empty, Toast.LENGTH_LONG).show();

            } else if ((ENo.getText().toString().length() < 11))

            {
                ENo.setError(getText(R.string.Invalid));
                Toast.makeText(this, R.string.Invalid, Toast.LENGTH_LONG)
                        .show();

            }

            else

            {
                s1 = "000";
                s2 = "00000000";
                throw new Exception();
            }

        }

        catch (Exception e) {
            e.printStackTrace();

            s1 = enr.substring(0, 3);
            s2 = enr.substring(3, 11);
            s3 = "13202709";
            int r1 = s1.compareTo(n1);
            int r2 = s1.compareTo(n2);

            if (s2.equals(s3)) {
                if (r1 == 0 || r2 == 0) {

                    Toast.makeText(this, R.string.Success, Toast.LENGTH_LONG)
                            .show();
                    val=new ContentValues();
                    val.clear();
                    val.put(obj.C_ID, enr);
                    val.put(obj.C_Name, name);
                    val.put(obj.C_ENo,enr);
                    db.insert(obj.TABLE, null, val);
                    db.close();




                } else if (r1 < 0 || r2 > 0) {
                    Toast.makeText(this, R.string.Fail, Toast.LENGTH_LONG)
                            .show();
                } else if (r1 > 0 || r2 < 0) {
                    Toast.makeText(this, R.string.Success, Toast.LENGTH_LONG)
                            .show();
                }
            }

            else

                Toast.makeText(this, R.string.Fail, Toast.LENGTH_LONG).show();

        }
        db.close();
        obj.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.item1:
            startActivity(new Intent(this, Welcomescreen.class));
            this.finish();
            break;

        case R.id.item2:
            this.finish();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, Welcomescreen.class);
        // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        this.finish();
    }

}

Recommended Answers

All 2 Replies

Simple you are missing content provider. Secondly what the heck you doing in that catch statment of onlick of the second class!!?? Have look at this tutorial how to do it properly

Sir actually i have designed my code so that the control will definitely goes to catch block and then perform normally.I know its an unorthodox approach but i'll change it later.
thanks for your concern
I'll try to implement content provider

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.