我从json字符串获取值并将其添加到arraylist getSpinArrList中。我已发布相关代码。
我的问题是,Spinner没有显示第一个项目而且OnItemClick不能在微调器中工作。
但如果我点击微调器,我只能查看我从arraylist获得的项目。
MainActivity.java:
Spinner spinCrePage;
spinCrePage = (Spinner) findViewById(R.id.sp_create_page);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getSpinArrList);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCrePage.setAdapter(adapter_state);
spinCrePage.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getApplicationContext(),"Working" , Toast.LENGTH_SHORT).show();
spinCrePage.setSelection(position);
String selState = (String) spinCrePage.getSelectedItem();
// textView.setText("Selected Android OS:" + selState);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
答案 0 :(得分:1)
onItemSelected,您必须使用该位置创建一个开关案例。对于每个位置创建一个案例并使用你的代码实现它......
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:
Toast.makeText(getApplicationContext(),"Working" , Toast.LENGTH_SHORT).show();
break;
case 1:
//code
break;
}
}
答案 1 :(得分:0)
请使用此代码并按照以下步骤操作
- Spinner中的项目列表 打开“res / values / strings.xml”文件,定义将在Spinner(下拉列表)中显示的项目列表。
醇>
文件:res / values / strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyAndroidApp</string>
<string name="country_prompt">Choose a country</string>
<string-array name="country_arrays">
<item>Malaysia</item>
<item>United States</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
<item>India</item>
</string-array>
- 创建一个xml文件res / layout / main.xml 在Spinner条目中使用country_arrays
醇>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/country_arrays"
android:prompt="@string/country_prompt" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
- 代码 创建Java文件MyAndroidAppActivity.java
醇>
package com.webastral.drinkin.home;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MyAndroidAppActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(
MyAndroidAppActivity.this,
"OnClickListener : " + "\nSpinner 1 : "
+ String.valueOf(spinner1.getSelectedItem())
+ "\nSpinner 2 : "
+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}
}
创建Java文件CustomOnItemSelectedListener.java
package com.webastral.drinkin.home;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(
parent.getContext(),
"OnItemSelectedListener : "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Hope This wil Help you
答案 2 :(得分:0)
在您的应用程序中,我认为只有当您连续两次选择相同的项目时,烤面包没有出现的唯一时间。我试用了你的代码,它对我来说很好。您可能想要删除
spinCrePage.setSelection(position);
来自您的代码。它根本没用。 仅当您从微调器中的上一个项目中选择不同的项目时,才会触发OnItemSelected()。
就“Spinner没有显示第一项”的问题而言,你应该查看你的arraylist的内容。查看执行期间是否有任何内容被删除。检查用于将数据添加到arraylist的循环。
“OnClick not working”:我很确定Spinner没有OnClickListener,Spinners使用OnItemSelectedListener()。