0

我有一个显示我的数组数据列表和删除按钮的微调器。我想要做的是当我单击删除按钮时,它会自动删除选定的微调器值,但我不确定如何执行此操作。

在删除按钮单击功能中,再次调试我的活动后删除了选定的微调器值,但我想在单击删除按钮时自动删除选定的微调器值。

package quesansw.the1;

import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class View1 extends Activity { 
    SQLiteDatabase db;
    ArrayAdapter adapter; 
    private String array_spinner[];

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db = openOrCreateDatabase("mydatabase.db",
                SQLiteDatabase.CREATE_IF_NECESSARY, null);

        final Dialog d1 = new Dialog(this);
        Window window = d1.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        //d1.setTitle("Login");

        d1.setContentView(R.layout.view);
        d1.show();
        Button Click = (Button) d1.findViewById(R.id.Click);
        Button Save = (Button) d1.findViewById(R.id.Save);
        Button Delete = (Button) d1.findViewById(R.id.Delete);

        ArrayList<String> list = new ArrayList<String>();
        Cursor cursor = db.rawQuery("select * from records", null);
        list.add("");
        if (cursor.moveToFirst())
        {
            do
            {
                list.add(cursor.getString(0));
            }
            while (cursor.moveToNext());
        }

        /*array_spinner=new String[20];
        array_spinner[0]=list.get(0);*/
        Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
        adapter = new ArrayAdapter<Object>(this,android.R.layout.simple_spinner_item, list.toArray());

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


        /*Save.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
                String str = s.getSelectedItem().toString();
                System.out.println("********"+str);
                Cursor cur1=db.rawQuery("select * from records where tittle='"+str+"' ",null);
                cur1.moveToNext();
                String str1=cur1.getString(1);

                EditText ans = (EditText) d1.findViewById(R.id.text);
                ans.setText(str1);

            }
            });*/

        Delete.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
                String str = s.getSelectedItem().toString();
                db.execSQL("delete from records where tittle='"+str+"' ");

            }
            });


        Click.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
                String str = s.getSelectedItem().toString();
                System.out.println("********"+str);
                Cursor cur1=db.rawQuery("select * from records where tittle='"+str+"' ",null);
                cur1.moveToNext();
                String str1=cur1.getString(1);

                EditText ans = (EditText) d1.findViewById(R.id.text);
                ans.setText(str1);


            }
            });
    }
}

1)这是我的活动截图

活动截图

2)另一个带有微调器值的屏幕截图:

微调器值

4

3 回答 3

0

在 deleteButton 的 onClickListener 中,从数组数据列表中删除选定的微调器值,并在删除后使用新的数组列表调用 onnotifydatasetChanges 或再次 setSpinner 适配器。

public class MainActivity extends Activity {

Spinner spin;
Button delete;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    delete  = (Button) findViewById(R.id.button1);
    spin = (Spinner) findViewById(R.id.spinner1);

    final ArrayList<String> spinneritems = new ArrayList<String>();

    spinneritems.add("item 1");
    spinneritems.add("item 2");
    spinneritems.add("item 3");
    spinneritems.add("item 4");
    spinneritems.add("item 5");


    final ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinneritems );
    adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(adp);

    delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String selectedSpinner = spin.getSelectedItem().toString();
            spinneritems.remove(selectedSpinner);
            adp.notifyDataSetChanged();
        }
    });
}
}
}
于 2013-03-28T13:03:57.077 回答
0

你为什么要Spinner多次声明和初始化你的?只做一次

public class View1 extends Activity { 
    SQLiteDatabase db;
    ArrayAdapter adapter; 
    private String array_spinner[];
    Spinner spinner;               // Declare your spinner here

然后在onCreate()

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db = openOrCreateDatabase("mydatabase.db",
            SQLiteDatabase.CREATE_IF_NECESSARY, null);
        spinner = (Spinner) d1.findViewById(R.id.tittle_spinner);  //initialize your spinner here

        final Dialog d1 = new Dialog(this);

然后当您单击delete button呼叫adapter.notifyDataSetChanged()以更新您的Array和微调器时

于 2013-03-28T13:05:17.727 回答
0

在删除按钮上单击侦听器添加以下代码

Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
String str = s.getSelectedItem().toString();
adapter.remove(str);
adapter.notifyDataSetChanged();
s.setSelection(0);
于 2013-03-28T13:21:27.657 回答