2 Replies - 3819 Views - Last Post: 07 October 2013 - 02:28 PM

#1 SBUSISO  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-March 13

android SQLite database, problem retrieving entered data

Posted 07 October 2013 - 01:44 PM

my program output an error message which says unfortunately the unizulu gumtree(unizulu gumtree is the name of the project) is not working when i'm clicking the button which will show the entered data on the table.

package com.example.gumtree;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Book extends Activity {
	String booktitle,bookprice,phonenumber;
	SQLiteDatabase db;
    TableRow tableRow;
	TextView textView,textView1,textView2,textView3,textView4,textView5;
	private EditText title,price,phone;
   
    private Button postButton;
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.book);
        db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Book(booktitle VARCHAR,bookprice VARCHAR, phonenumber INTEGER(10));");
        registerViews();
    }
public void data(View view)
{
  EditText edittext1=(EditText )findViewById(R.id.firstname);
  EditText edittext2=(EditText )findViewById(R.id.lastname);
  EditText edittext3=(EditText )findViewById(R.id.email);
  booktitle=edittext1.getText().toString();
  bookprice=edittext2.getText().toString();
  phonenumber=edittext3.getText().toString();
  db.execSQL("INSERT INTO  Book VALUES('"+booktitle+"','"+bookprice+"','"+phonenumber+"');");
	
	 
}
public void showdata(View view)
{
    Cursor c=db.rawQuery("SELECT * from Book", null);
     int count= c.getCount();
    c.moveToFirst();
    TableLayout tableLayout = new TableLayout(getApplicationContext());
    tableLayout.setVerticalScrollBarEnabled(true);
   TableRow tableRow;
   TextView textView,textView1,textView2,textView3,textView4,textView5;
   tableRow = new TableRow(getApplicationContext());
   textView=new TextView(getApplicationContext());
   textView.setText("Title");
   textView.setTextColor(Color.RED);
  	textView.setTypeface(null, Typeface.BOLD);
  	 textView.setPadding(20, 20, 20, 20);
  	tableRow.addView(textView);
    textView4=new TextView(getApplicationContext());
  	textView4.setText("Price");
  	textView4.setTextColor(Color.RED);
  	textView4.setTypeface(null, Typeface.BOLD);
  	 textView4.setPadding(20, 20, 20, 20);
  	tableRow.addView(textView4);
    textView5=new TextView(getApplicationContext());
  	textView5.setText("PhoneNo");
  	textView5.setTextColor(Color.RED);
  	textView5.setTypeface(null, Typeface.BOLD);
  	textView5.setPadding(20, 20, 20, 20);
  	tableRow.addView(textView5);
   tableLayout.addView(tableRow);
     for (Integer j = 0; j < count; j++)
     {
         tableRow = new TableRow(getApplicationContext());
         textView1 = new TextView(getApplicationContext());
         textView1.setText(c.getString(c.getColumnIndex("booktitle")));
         textView2 = new TextView(getApplicationContext());
         textView2.setText(c.getString(c.getColumnIndex("bookprice")));
         textView3 = new TextView(getApplicationContext());
         textView3.setText(c.getString(c.getColumnIndex("phonenumber")));
         textView1.setPadding(20, 20, 20, 20);
         textView2.setPadding(20, 20, 20, 20);
         textView3.setPadding(20, 20, 20, 20);
         tableRow.addView(textView1);
         tableRow.addView(textView2);
         tableRow.addView(textView3);
         tableLayout.addView(tableRow);
         c.moveToNext() ;
     }
     setContentView(tableLayout);
db.close();
}

// validate text fields
private void registerViews() {
    title = (EditText) findViewById(R.id.firstname);
    // TextWatcher would let to check validation error on the fly
    title.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            Validation.hasText(title);
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });

    price = (EditText) findViewById(R.id.lastname);
    price.addTextChangedListener(new TextWatcher() {
        // after every change has been made to this editText, we would like to check validity
        public void afterTextChanged(Editable s) {
            Validation.isPrice(price, true);
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });

    phone = (EditText) findViewById(R.id.email);
    phone.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            Validation.isPhoneNumber(phone, false);
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });

    postButton = (Button) findViewById(R.id.buttonP);
    postButton.setonclickListener(new View.onclickListener() {
        @Override
        public void onclick(View view) {
            /*
            Validation class will check the error and display the error on respective fields
            but it won't resist the form submission, so we need to check again before submit
             */
            if ( checkValidation () )
                submitForm();
            else
                Toast.makeText(Book.this, "INCORRECT INPUTS", Toast.LENGTH_LONG).show();
        }
    });
}

private void submitForm() {
    // Submit your form here. your form is valid
    Toast.makeText(this, "Advert Has Been Posted", Toast.LENGTH_LONG).show();
}

private boolean checkValidation() {
    boolean ret = true;

    if (!Validation.hasText(title)) ret = false;
    if (!Validation.isPrice(price, true)) ret = false;
    if (!Validation.isPhoneNumber(phone, false)) ret = false;

    return ret;
}

//end of text fields validation

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
   
    public void handleClick1(View v){
    	Intent intent = new Intent ();
    	intent.setClass (this,Book.class);
    	startActivity(intent);
    }

}


and the xml code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rtv"
    android:gravity="right" >

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:onclick="showdata"
        android:text="@string/sho"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="@string/descr"
        android:textColor="#ffffff"
        tools:ignore="HardcodedText" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/b"
        tools:ignore="ContentDescription" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:textColor="#ffffff"
        android:text="@string/adv"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/buttonP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:onclick="data"
        android:text="@string/post"
        tools:ignore="HardcodedText" />

    <EditText
        android:id="@+id/firstname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lastname"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView1"
        android:background="#ffffff"
        android:ems="10"
        android:inputType="text"
        tools:ignore="TextFields" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/lastname"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/firstname"
        android:layout_marginTop="30dp"
        android:text="@string/p"
        android:textColor="#ffffff"
        tools:ignore="HardcodedText" />

    <EditText
        android:id="@+id/lastname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/firstname"
        android:layout_marginTop="31dp"
        android:background="#ffffff"
        android:ems="10"
        android:inputType="text"
        tools:ignore="TextFields" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/email"
        android:text="@string/phone"
        android:textColor="#ffffff"
        tools:ignore="HardcodedText" />

    <EditText
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttonP"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="19dp"
        android:background="#ffffff"
        android:ems="10"
        android:inputType="number"
        tools:ignore="TextFields" >

        <requestFocus />
    </EditText>

</RelativeLayout>





Is This A Good Question/Topic? 0
  • +

Replies To: android SQLite database, problem retrieving entered data

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,844
  • Joined: 12-December 12

Re: android SQLite database, problem retrieving entered data

Posted 07 October 2013 - 01:52 PM

Post the complete error message and tell us which line it refers to in your posted code. Or, at least, post the error message and tell us whereabouts in your code you think the problem is.

This post has been edited by andrewsw: 07 October 2013 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 SBUSISO  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-March 13

Re: android SQLite database, problem retrieving entered data

Posted 07 October 2013 - 02:28 PM

it just shows the error message (unfortunately the unizulu gumtree is not working) i think the problem is with the method Showdata because after it has output the error message it then highlight this line of code
textView1.setText(c.getString(c.getColumnIndex("booktitle")));

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1