-4

我的程序有一个活动和一个数据库类。有一个用于从数据库中检索数据的按钮。我的意思是在textView. 但它没有得到。

第一活动.java

package example.showevent1;

    import java.util.Calendar;

    import android.os.Bundle;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.support.v4.app.*;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemSelectedListener;

    public class FirstActivity extends FragmentActivity implements OnItemSelectedListener {
        /** Called when the activity is first created. */

        classdbOpenHelper eventsData;
        classdbOpenHelper eventsData1;

         TextView userSelection;
         Button okButton;
         Button addButton;

         Button change_date_but;
         TextView date;
         TextView show;
         EditText edittext;
          public static final int Date_dialog_id = 1;
          private int mYear;
          private int mMonth;
          private int mDay;

        private static final String[] items={"Yalahanka","Rajai nagar","Sivaji Nagar","Koramangala","RT Nagar", "Banashankari","Yashwanthpura","Hebbal"};



        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
             okButton = (Button)findViewById(R.id.button2);
             addButton = (Button)findViewById(R.id.button3);

             change_date_but = (Button)findViewById(R.id.button1);
             date = (TextView)findViewById(R.id.textView2);//KEY_DATE
             userSelection=(TextView)findViewById(R.id.textView1);//KEY_DESC
             edittext=(EditText)findViewById(R.id.editText1);//KEY_EVENT
             show=(TextView)findViewById(R.id.textView5);

             Spinner my_spin=(Spinner)findViewById(R.id.spinner1);
            my_spin.setOnItemSelectedListener(this);
            ArrayAdapter aa=new ArrayAdapter(this, android.R.layout.simple_spinner_item,items);
            aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            my_spin.setAdapter(aa);

            okButton.setOnClickListener(new clicker());
            addButton.setOnClickListener(new getclicker());

            eventsData = new classdbOpenHelper(this);

            change_date_but.setOnClickListener(new View.OnClickListener() {



                       @Override
                       public void onClick(View v) {
                         DatePickerDialog DPD = new DatePickerDialog(
                       FirstActivity.this, mDateSetListener, mYear, mMonth,mDay);
                                    DPD.show();
                   }
                      });
            final Calendar c = Calendar.getInstance();
                  mYear = c.get(Calendar.YEAR);
                  mMonth = c.get(Calendar.MONTH);
                  mDay = c.get(Calendar.DAY_OF_MONTH);

                  updateDisplay();
        }
         @Override
             @Deprecated

             protected void onPrepareDialog(int id, Dialog dialog) {
              // TODO Auto-generated method stub
           super.onPrepareDialog(id, dialog);

              ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);

             }

             private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

           public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
               mYear = year;
               mMonth = monthOfYear;
           mDay = dayOfMonth;
               updateDisplay();
              }
             };

             private void updateDisplay() {
           // TODO Auto-generated method stub
              date.setText(new StringBuilder()
                // Month is 0 based so add 1
             .append(mMonth + 1).append("-").append(mDay).append("-")
                .append(mYear));
             }







        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            userSelection.setText(items[pos]);
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            userSelection.setText("");
        }



        class clicker implements Button.OnClickListener {
            public void onClick(View v) {
                String datevalue = date.getText().toString();
                String Userselectvalue = userSelection.getText().toString();
                String Userevent = edittext.getText().toString();
                SQLiteDatabase  db = eventsData.getWritableDatabase();

                ContentValues cv = new ContentValues();
                cv.put(classdbOpenHelper.KEY_DESC, Userselectvalue);
                cv.put(classdbOpenHelper.KEY_EVENT, Userevent);
                cv.put(classdbOpenHelper.KEY_DATE,datevalue);
                db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv);
                db.close();
            }


        public void onDestroy() {
          eventsData.close();
        }

    }
        class getclicker implements Button.OnClickListener {
            public void onClick(View v) {
                String datevalue = date.getText().toString();
                String Userselectvalue = userSelection.getText().toString();

                String showevent = eventsData.getContact(datevalue,Userselectvalue);
                show.setText(showevent);
            }
            public void onDestroy() {
                eventsData.close();
              }

        }

    }

类dbOpenHelper.java

package example.showevent1;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.TextView;


public class classdbOpenHelper extends SQLiteOpenHelper {

    public static final String KEY_ROWID = "id";
    public static final String KEY_DESC = "countdesc";
    public static final String KEY_EVENT = "countevent";
    public static final String KEY_DATE = "countdate";

    public static final String DATABASE_NAME= "countdb";
    public static final String DATABASE_TABLE = "countable";
    public static final int DATABASE_VERSION = 1;


    public classdbOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
       /* db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
        //KEY_COUNTED + " INTEGER " +
        KEY_DESC + " TEXT NOT NULL " +
        KEY_DATE + " TEXT NOT NULL);"
                ); */
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + DATABASE_TABLE + "("
                + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESC + " TEXT, " + KEY_EVENT + " TEXT, " + KEY_DATE + " TEXT " + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXITS " + DATABASE_TABLE);
        onCreate(db);

    }

    public String getContact(String datevalue,String Userselectvalue) 
    {
         String place = null;
      String selection = classdbOpenHelper.KEY_DESC + " = 'Userselectvalue'"  
           +" AND " + classdbOpenHelper.KEY_DATE + " = 'datevalue'";
      SQLiteDatabase db = this.getReadableDatabase(); 
      Cursor cursor = db.query(classdbOpenHelper.DATABASE_TABLE, 
           new String[] {classdbOpenHelper.KEY_EVENT }, selection, 
           null, null, null, null);
      if (cursor .getCount() > 0) {
           cursor .moveToFirst();
           do {
                  place = cursor.getString(0);
           } while (cursor.moveToNext());
       }
      cursor.close();
    return place;  
    }
}

activity_first.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/color_1"
    tools:context=".FirstActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner1"
        android:layout_below="@+id/spinner1"
        android:layout_marginTop="28dp"
        android:background="#ffff99"
        android:text="@string/dt"
        android:textColor="#b22924" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="31dp"
        android:text="@string/select"
        android:textAlignment="gravity"
        android:textColor="#b22924"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="36dp"
        android:background="#ffff99"
        android:textColor="#b22924" 
        android:fadingEdge=""/>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="40dp"
        android:background="#ffff99"
        android:ems="10" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentRight="true"
        android:text="@string/_add" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentRight="true"
        android:background="#ffff99"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:text="Select A Place" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_alignTop="@+id/button2"
        android:text="Add Event" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/button2"
        android:layout_marginTop="27dp"
        android:text="Show Event" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="22dp"
        android:background="#ffff99"
        android:text="" />

</RelativeLayout>

字符串.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <string name="app_name">EVENTS</string>
    <string name="action_settings">Settings</string>
    <string name="select">SELECT A PLACE</string>
    <color name="color_1">#ffcccccc</color>
    <string name="dt">date</string>
    <string name="_add">Add</string>


</resources>
4

1 回答 1

0

您的问题如下:您没有从数据库中获得任何结果:

修改以下行

String selection = classdbOpenHelper.KEY_DESC + " = 'Userselectvalue'" +" AND " + classdbOpenHelper.KEY_DATE + " = 'datevalue'";

String selection = classdbOpenHelper.KEY_DESC + " = '" + Userselectvalue + "'" + " AND " + classdbOpenHelper.KEY_DATE + " = '" + datevalue+ "'";

应该可以解决您的问题。

我建议你阅读一些教程:

  • 代码风格
  • 变量命名
  • 基本调试(你会在几分钟内发现你的错误)
于 2013-08-05T13:33:54.187 回答