gpt4 book ai didi

java - SQLite 的 Android Spinner 表现得很奇怪

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:53 27 4
gpt4 key购买 nike

我有 3 个旋转器

1) 数组旋转器

2) XML Spinner 和

3) SQLite 光标微调器

当我添加第三个 Spinner 时,页面开始变得奇怪。旋转器 1 和 2 开始出现在旋转器 3 区域中。 Cursor 中的值出现在 Spinner 3 中。当我选择任何值时,TOAST 工作正常,但未选择该值。

布局文件:spinner_demo_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.training.sqlitelistviewdemo.SpinnerDemo">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Country"
android:textColor="#000000"
android:textSize="20sp" />

<Spinner
android:id="@+id/spCountries"
style="@android:style/Widget.Holo.Light.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/country_arrays"
android:prompt="@string/country_prompt"
android:spinnerMode="dialog">

</Spinner>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Business Type"
android:textColor="#000000"
android:textSize="20sp" />

<Spinner
android:id="@+id/spBussinessType"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/business_prompt">

</Spinner>


<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Shop Name"
android:textColor="#000000"
android:textSize="20sp" />

<Spinner
android:id="@+id/sp_Shops"
style="@android:style/Widget.Holo.Light.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/select_shops"
android:spinnerMode="dialog">
</Spinner>


<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Submit"
android:textSize="15sp" />

我的 Java 文件:SpinnerDemo.java

package com.training.sqlitelistviewdemo;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SpinnerDemo extends AppCompatActivity implements View.OnClickListener {

// uicontrols
Spinner spCountries;
Spinner spBusinessType;
Button btnsubmit;

//class members
String businessType[] = { "Automobile", "Food", "Computers", "Education", "Personal", "Travel" };
ArrayAdapter<String> adapterBusinessType;

// local members
String sbusinesstype,scountry;

// SQLite Parameters Start

private DatabaseManager dbManager;
private TextView tv_Text;
private Spinner sp;

private SimpleCursorAdapter adapter;

// final String[] from = new String[] { DatabaseHelper._ID, DatabaseHelper.SHOPNAME, DatabaseHelper.SHOPADDRESS };
final String[] from = new String[] {DatabaseHelper.SHOPNAME};

//final int[] to = new int[] { R.id.id, R.id.shopname, R.id.shopaddress };
final int[] to = new int[] {android.R.id.text1};

// SQLite Parameters End


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_demo_main);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);

spCountries = (Spinner) findViewById(R.id.spCountries);
spBusinessType = (Spinner) findViewById(R.id.spBussinessType);

btnsubmit=(Button)findViewById(R.id.submit);
btnsubmit.setOnClickListener(this);

// Initialize and set Adapter
adapterBusinessType = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, businessType);
adapterBusinessType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spBusinessType.setAdapter(adapterBusinessType);

// Country Item Selected Listener
spCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
scountry = adapter.getItemAtPosition(position).toString();

// Showing selected spinner item
Toast.makeText(getApplicationContext(),
"Selected Country : " + scountry, Toast.LENGTH_LONG).show();
}

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

}
});
// Business Type Item Selected Listener
spBusinessType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
sbusinesstype = adapter.getItemAtPosition(position).toString();

// Showing selected spinner item
Toast.makeText(getApplicationContext(),
"Bussiness Type : " + sbusinesstype, Toast.LENGTH_LONG).show();
}

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

}
});
// SQLite Spinner Code Starts

dbManager = new DatabaseManager(this);
dbManager.open();
Cursor cursor = dbManager.fetch_Shop();
sp = (Spinner) findViewById(R.id.sp_Shops);
// sp.setEmptyView(findViewById(R.id.empty));

adapter = new SimpleCursorAdapter(this, R.layout.spinner_demo_main, cursor, from, to, 0);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
//sp.setSelection(1);
//adapter.notifyDataSetChanged();

sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
sp.setSelection(position);

Cursor shopCur=(Cursor)sp.getSelectedItem();
String shopNamecol=shopCur.getString(shopCur.getColumnIndex(DatabaseHelper.SHOPNAME));
String Rowid = shopCur.getString(shopCur.getColumnIndex(DatabaseHelper._ID));
Toast.makeText(getApplicationContext(), "You Selected - " + shopNamecol + " - " + Rowid + " " + position, Toast.LENGTH_LONG).show();
}

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

}
});



// SQLite Spinner Code Ends
}

@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "You have selected " + scountry + " and " + sbusinesstype,Toast.LENGTH_LONG).show();
}

}

Spinner Screen Shot

任何帮助将不胜感激。

最佳答案

要解决此问题,请在 java 文件中声明三个微调器并在 java 文件中初始化它们

Spinner spCountries;
Spinner spBusinessType;
Spinner shops;

//class members
String businessType[] = { "Automobile", "Food", "Computers", "Education", "Personal", "Travel" };
ArrayAdapter<String> adapterBusinessType;
ArrayAdapter<String> adapterspCountriesType;
ArrayAdapter<String> adaptershopsType;

现在添加 setOnItemSelectedListener 并处理 using switch case 。

对我来说效果很好。

关于java - SQLite 的 Android Spinner 表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42712385/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com