OnItemSelected不适用于值来自SQLite数据库的自定义微调器

时间:2019-06-05 06:33:25

标签: android

我是Android Studio的新手,我正在一个项目中,该项目需要一个带有从SQLite填充值的微调器。我需要根据用户从微调器中所做的选择来更改图像(图标)。

我尝试为自定义布局上的TextView制作android:clickable =“ false”。但这并不能解决。每当用户触摸微调框时,我还使用了spinner.setOnTouchListener来更新列表。

categoryspinner_layout.xml(自定义布局)

    <TextView
        android:id="@+id/category"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium">

    </TextView>

</LinearLayout>

微调框布局。 (具有多个微调器,但没有自定义布局)

     <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewTransfers">

    <TextView
        android:id="@+id/filterDateStart"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Date"
        android:inputType="date"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3" />

    <Button
        android:id="@+id/addFilterDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="ADD DATE"
        app:layout_constraintStart_toEndOf="@+id/filterDateStart"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3" />


    <TextView
        android:id="@+id/filterMode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Recurring"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mop_viewTransfer" />

    <Button
        android:id="@+id/viewTranfers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="56dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="View Transfer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.477"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recurring_viewTransfer" />

    <EditText
        android:id="@+id/deleteCatID"
        android:layout_width="124dp"
        android:layout_height="66dp"
        android:layout_marginStart="28dp"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="70dp"
        android:ems="10"
        android:hint="Enter an ID"
        android:inputType="number"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/viewTranfers" />

    <Button
        android:id="@+id/delTransfer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="106dp"
        android:layout_marginLeft="106dp"
        android:layout_marginTop="70dp"
        android:layout_marginEnd="73dp"
        android:layout_marginRight="73dp"
        android:text="DELETE"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/deleteCatID"
        app:layout_constraintTop_toBottomOf="@+id/viewTranfers" />

    <Spinner
        android:id="@+id/mop_viewTransfer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <Spinner
        android:id="@+id/recurring_viewTransfer"
        android:layout_width="wrap_content"
        android:layout_height="28dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/filterMode" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="166dp"
        android:layout_height="25dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="Mode of payment"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/amount_filter" />

    <EditText
        android:id="@+id/amount_filter"
        android:layout_width="142dp"
        android:layout_height="42dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="52dp"
        android:layout_marginRight="52dp"
        android:ems="10"
        android:hint="Amount"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toStartOf="@+id/currency_filter"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/addFilterDate" />

    <Spinner
        android:id="@+id/currency_filter"
        android:layout_width="96dp"
        android:layout_height="41dp"
        android:layout_marginStart="52dp"
        android:layout_marginLeft="52dp"
        app:layout_constraintStart_toEndOf="@+id/amount_filter"
        app:layout_constraintTop_toBottomOf="@+id/addFilterDate" />

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginStart="12dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Spinner
            android:id="@+id/filterCategory"
            android:layout_width="299dp"
            android:layout_height="41dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPersonName" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="46dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:src="@drawable/imageview1" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

OnItemSelected

 package com.example.watch;

import android.app.DatePickerDialog;
import android.database.Cursor;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class ViewTransfers extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, AdapterView.OnItemSelectedListener {
    EditText deleteIDs, amount;
    TextView startDate;
    Button view, delete , addDate;
    Spinner mop_ViewTransfer,recurring_ViewTransfer,currency,category;
    DatabaseHelper myDb;
    DataHelper cDb;


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

        myDb = new DatabaseHelper(this);


        deleteIDs=(EditText) findViewById(R.id.deleteCatID);
        category =(Spinner) findViewById(R.id.filterCategory);
        startDate =(TextView) findViewById(R.id.filterDateStart);
        amount=(EditText) findViewById(R.id.amount_filter);
        currency=(Spinner) findViewById(R.id.currency_filter);
        recurring_ViewTransfer=(Spinner) findViewById(R.id.recurring_viewTransfer);
        mop_ViewTransfer=(Spinner) findViewById(R.id.mop_viewTransfer);
        view = (Button) findViewById(R.id.viewTranfers);
        delete=(Button) findViewById(R.id.delTransfer);
        addDate = (Button) findViewById(R.id.addFilterDate);

        implementValue();

        addDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment datePicker=new DatePickerFrangment();
                datePicker.show(getSupportFragmentManager(),"date picker");
            }
        });



        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String categoryS , startDateS, recurrentS, modeS,amountS,currencyS;
                categoryS = category.getSelectedItem().toString();
                startDateS= startDate.getText().toString();
                amountS=amount.getText().toString();
                currencyS=currency.getSelectedItem().toString();
                modeS= mop_ViewTransfer.getSelectedItem().toString();
                recurrentS= recurring_ViewTransfer.getSelectedItem().toString();

                if (amountS.length()!=0) amountS = amountS+" "+currencyS;

                Cursor res = myDb.getFilteredData( categoryS , startDateS,amountS, modeS,recurrentS);
                if (res.getCount() == 0) {
                    showMessage("Error", "No records found!");
                    return;
                }
                StringBuffer buffer = new StringBuffer();
                while (res.moveToNext()) {
                    buffer.append("ID : " + res.getString(0) + "\n");
                    buffer.append("Category : " + res.getString(1) + "\n");
                    buffer.append("Date : " + res.getString(2) + "\n");
                    buffer.append("Amount : " + res.getString(3) + "\n");
                    buffer.append("Payment : " + res.getString(4) + "\n");
                    buffer.append("Recurring : " + res.getString(5) + "\n");
                    buffer.append("Comments : " + res.getString(6) + "\n \n");

                }
                showMessage("Money Tracker", buffer.toString());

            }
        });

        delete.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                String text =deleteIDs.getText().toString();
                if (text.length() != 0) {
                    Integer IDs = Integer.parseInt(text) ;
                    boolean check = myDb.deleteTransfer(IDs);
                    if (check == true) {
                        Toast.makeText(ViewTransfers.this, "Data deleted!", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(ViewTransfers.this, "Something went wrong!", Toast.LENGTH_LONG).show();
                    }

                }
                else {
                    Toast.makeText(ViewTransfers.this,"Enter a valid ID!",Toast.LENGTH_LONG).show();
                }
            }
        });


    }
    public void showMessage(String title, String Message){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(Message);
        builder.show();
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR,year);
        c.set(Calendar.MONTH,month);
        c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
        String currentDateString= DateFormat.getDateInstance().format(c.getTime());
        startDate.setText(currentDateString); }

    public void implementValue(){
        Spinner recurring_ViewTransfer = findViewById(R.id.recurring_viewTransfer);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.transaction, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        recurring_ViewTransfer.setAdapter(adapter);
        recurring_ViewTransfer.setOnItemSelectedListener(this);

        Spinner mop_ViewTransfer = findViewById(R.id.mop_viewTransfer);
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
                R.array.mop, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mop_ViewTransfer.setAdapter(adapter2);
        mop_ViewTransfer.setOnItemSelectedListener(this);

        Spinner currency = findViewById(R.id.currency_filter);
        ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this,
                R.array.currency, android.R.layout.simple_spinner_item);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        currency.setAdapter(adapter3);
        currency.setOnItemSelectedListener(this);

        categoryspin();
     /*   category.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                categoryspin();

                return false;
            }
        });
*/
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       switch (parent.getId()) {
           case R.id.filterCategory : {
               String category1 = category.getSelectedItem().toString();
                Toast.makeText(ViewTransfers.this, category1, Toast.LENGTH_SHORT).show();
               /*String category1 = category.getSelectedItem().toString();
               String img = cDb.getIcon(category1);

               imgIcon = (ImageView) findViewById(R.id.imageView);

               int resID = getResources().getIdentifier(img, "drawable", getPackageName());
               imgIcon.setImageResource(resID);*/
           }
       }

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    public  void categoryspin(){

        cDb = new DataHelper(this);
        ArrayList<String> list = cDb.getAllCategories();

        ArrayAdapter<String> adapter3 = new ArrayAdapter<>(this, R.layout.categoryspinner_layout, R.id.category, list);
        category.setAdapter(adapter3);
    }


}



There ain't any error message. When  I open the page it gave me a toast message of the 1st entry in the spinner. After that nothing happens when I click on it.

1 个答案:

答案 0 :(得分:0)

我检查了您的代码,您必须根据当前的代码进行这样的编码,您已经在活动上下文中实现了事件。并且您还没有为事件侦听器编写代码。

public  void categoryspin(){

    cDb = new DataHelper(this);
    ArrayList<String> list = cDb.getAllCategories();

    ArrayAdapter<String> adapter3 = new ArrayAdapter<>(this, R.layout.categoryspinner_layout, R.id.category, list);
    category.setAdapter(adapter3);

     /*Add this line and it will start working with your existing code to handle spinner onItemSelected 
       event, Happy coding...*/
    category.setOnItemSelectedListener(this);

}

注意:尝试使用正确的命名约定,它将帮助您轻松地识别/查看代码并解决错误。

此外,此问题也不在于您从SQLite数据库获取的值。