1

データベースからスピナーデータをクリックすると、テキストが表示されます.しかし、私はそれを達成できませんでした.スピナーデータは、データベースを使用してスピナーに正常に追加されました. これが私の Java クラス ファイルです。onItemSelected リスナーのコーディングでエラーが発生しました。解決方法を教えてください。主な活動:

package com.samir.spinner;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public abstract class MainActivity extends Activity implements OnClickListener,
        OnItemSelectedListener {

    private EditText edittext;
    private Button btnAdd;
    private Spinner spinner;
    DatabaseHelper db;
    List<String> list;
    ArrayAdapter<String> adapter;
    TextView tv;
    Set<String> s;
    protected Cursor c;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        db = new DatabaseHelper(MainActivity.this);

        edittext = (EditText) findViewById(R.id.editText1);
        btnAdd = (Button) findViewById(R.id.btnadd);
        spinner = (Spinner) findViewById(R.id.spinner1);

        btnAdd.setOnClickListener(this);
        spinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onClick(View v) {

        String name = edittext.getText().toString().trim();

        if (TextUtils.isEmpty(name)) {
            // showToast("Please Enter Your Name");
            edittext.setError("Please Enter Your Name");
            edittext.requestFocus();
        } else {
            db.insertData(name);
            edittext.setText("");

            // Hiding the keyboard
            InputMethodManager inputmangager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmangager.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

            loadSpinner();
        }

    }

    private void loadSpinner() {
        // here i used Set Because Set doesn't allow duplicates.
        Set<String> set = db.getAllData();

        List<String> list = new ArrayList<String>(set);

        adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item, list);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);
        spinner.setWillNotDraw(false);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View v,
                    int position, long id) {

              Object item=parent.getItemAtPosition(position);
              tv=(TextView) findViewById(R.id.txt);
              tv.setText((CharSequence) item);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });
    }
}

これが私のlogcat出力です:LOGCAT:

08-05 08:34:51.647: E/AndroidRuntime(1586): FATAL EXCEPTION: main
08-05 08:34:51.647: E/AndroidRuntime(1586): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.samir.spinner/com.samir.spinner.MainActivity}: java.lang.InstantiationException: can't instantiate class com.samir.spinner.MainActivity
08-05 08:34:51.647: E/AndroidRuntime(1586):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at android.os.Looper.loop(Looper.java:137)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at java.lang.reflect.Method.invoke(Method.java:511)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at dalvik.system.NativeStart.main(Native Method)
08-05 08:34:51.647: E/AndroidRuntime(1586): Caused by: java.lang.InstantiationException: can't instantiate class com.samir.spinner.MainActivity
08-05 08:34:51.647: E/AndroidRuntime(1586):     at java.lang.Class.newInstanceImpl(Native Method)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at java.lang.Class.newInstance(Class.java:1319)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-05 08:34:51.647: E/AndroidRuntime(1586):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-05 08:34:51.647: E/AndroidRuntime(1586):     ... 11 more
08-05 08:37:09.820: I/Process(1586): Sending signal. PID: 1586 SIG: 9

これが私の DatabaseHelper クラスです。DatabaseHelper.java:

package com.samir.spinner;

import java.util.HashSet;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "mydb";
    private static final String TABLE_NAME = "mytable";
    private static final String _id = "_id";
    private static final String name = "name";

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableQuery = "create table " + TABLE_NAME + "(" + _id
                + " INTEGER PRIMARY KEY," + name + " TEXT)";
        db.execSQL(createTableQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("drop table if exists " + TABLE_NAME);

        onCreate(db);
    }

    public void insertData(String label) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(name, label);
        db.insert(TABLE_NAME, null, values);
        db.close();
    }


    public Set<String> getAllData() {
        Set<String> set = new HashSet<String>();

        String selectQuery = "select * from " + TABLE_NAME;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                set.add(cursor.getString(1));
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();

        return set;
    }
}
4

2 に答える 2

0

変化する

Object item=parent.getItemAtPosition(position);

から

String item = (String) parent.getItemAtPosition(position);

と単純に書く

tv.setText(item); rather then 


tv.setText((CharSequence) item); 
于 2013-08-05T08:47:32.820 に答える