0

我的程序包含一个活动和一个数据库类。我试图通过一个名为 getclicker 的内部类从活动类访问数据库元素,该类扩展了 listactivity。我也给了logcat。请帮我解决问题

FirstActivity.java 包example.showevent1;

import java.util.Calendar;

import android.os.Bundle;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.*;
import android.support.v4.widget.SimpleCursorAdapter;
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.ListView;
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;
    Cursor cursor1;
     TextView userSelection;
     Button okButton;
     Button addButton;

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

    private static final String[] items={"Yalahanka","Rajaji 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
         edittext.requestFocus();
         InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
         if(imm != null) {
             imm.showSoftInput(edittext, 0); 
         }

         show=(TextView)findViewById(R.id.textView5);
         listView=(ListView)findViewById(R.id.llList);

         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();
        }




}
    class getclicker extends ListActivity implements Button.OnClickListener {
        public void onClick(View v) {

            String datevalue = date.getText().toString();
            String Userselectvalue = userSelection.getText().toString();
             cursor1 = eventsData.getContact(datevalue,Userselectvalue);
             SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.events, cursor1, null, null);
                 listView = getListView();
                listView.setAdapter(adapter);
        }
        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;


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 Cursor getContact(String datevalue, String Userselectvalue){
        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);
        return cursor;
    }
}

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/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/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" />

    <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="#ffffff"
        android:text="" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView5"
        android:layout_alignLeft="@+id/textView5"
        android:layout_marginBottom="22dp"
        android:text="Show Event" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignLeft="@+id/button3"
        android:layout_marginBottom="17dp"
        android:background="#ffffff"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="16dp"
        android:background="#ffffff"
        android:textColor="#b22924" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_marginBottom="28dp"
        android:text="@string/select"
        android:textAlignment="gravity"
        android:textColor="#b22924"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/button2"
        android:background="#ffffff"
        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_above="@+id/spinner1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="22dp"
        android:text="Select A Place" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView4"
        android:layout_alignLeft="@+id/textView1"
        android:layout_marginBottom="38dp"
        android:text="@string/dt" />

</RelativeLayout>

事件.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
     <ListView
        android:id="@+id/llList"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_below="@+id/Button01" >
    </ListView>


</LinearLayout>

日志猫

08-06 07:41:33.680: E/AndroidRuntime(3482): FATAL EXCEPTION: main
08-06 07:41:33.680: E/AndroidRuntime(3482): java.lang.IllegalArgumentException: column '_id' does not exist
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:122)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:54)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:63)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at example.showevent1.FirstActivity$getclicker.onClick(FirstActivity.java:177)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.view.View.performClick(View.java:4204)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.view.View$PerformClick.run(View.java:17355)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.os.Handler.handleCallback(Handler.java:725)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.os.Looper.loop(Looper.java:137)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at java.lang.reflect.Method.invoke(Method.java:511)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-06 07:41:33.680: E/AndroidRuntime(3482):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

请检查 CursorAdapter 的文档,它说

“光标必须包含一个名为“_id”的列,否则此类将不起作用。”

因此,您需要查询 KEY_ROWID AS _id 列以使您的光标包含“_id”。

于 2013-08-06T08:34:21.977 回答