我的Spinner在填充数据后没有被选中,我尝试了一些来自堆栈溢出的修复,但没有它们有效。
我给Spinner的完整代码请找到这里发现的问题,谢谢.............
下面的spinner代码使用Asyntask onPost执行方法,但在Register Activity中不起作用。
package com.tutorials.hp.Tiktokads;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.Spinner;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.tutorials.hp.Tiktokads.m_DataObject.*;
import com.tutorials.hp.Tiktokads.m_DataObject.City;
import com.tutorials.hp.Tiktokads.m_MySQL.SpinCityDataDownload;
import com.tutorials.hp.Tiktokads.m_MySQL.SpinDownloader;
import com.tutorials.hp.Tiktokads.m_UI.AsyncResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class RegisterActivity extends AppCompatActivity { //implements AsyncResponse{
final static String urlAddress="projects/birthdays/android/getAllDistrict";
ArrayList<Province> ProvinceList = new ArrayList<>();
ArrayList<City> CityList = new ArrayList<>();
private Spinner sp;
private Spinner sp1;
private static final String TAG = "Naseemlog";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
sp= (Spinner) findViewById(R.id.sp);
sp1= (Spinner) findViewById(R.id.sp1);
sp1.setEnabled(false);
getData();
// new SpinDownloader(RegisterActivity.this,urlAddress,sp,sp1,RegisterActivity.this).execute();
//Log.i(TAG, String.valueOf(result));
ArrayAdapter adapter=new ArrayAdapter(RegisterActivity.this,android.R.layout.simple_spinner_dropdown_item,ProvinceList);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "sdasd");
//Toast.makeText(c,Provinces.get(position),Toast.LENGTH_SHORT).show();
Province province = (Province) parent.getSelectedItem();
//Toast.makeText(c, "Province ID: "+province.getId()+", Province Name : "+province.getName(), Toast.LENGTH_SHORT).show();
// Message message = new Message();
// Bundle bundle = new Bundle();
// bundle.putString("District", province.getId());
// message.setData(bundle);
// handler.sendMessage(message); // pass handler object from activity
String district=province.getId();
String urlAddress1="projects/birthdays/android/getAllCityByDistrict/"+province.getId();
Log.i(TAG, urlAddress1);
//new SpinCityDataDownload(c,urlAddress1,sp,sp1,mCallback).execute();
getCityData(urlAddress1);
// sp1.setEnabled(true);
// //BIND
// ArrayAdapter adapter1=new ArrayAdapter(RegisterActivity.this,android.R.layout.simple_spinner_dropdown_item,CityList);
// sp1.setAdapter(adapter1);
//
// sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
// @Override
// public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// //Toast.makeText(c,Provinces.get(position),Toast.LENGTH_SHORT).show();
// City city = (City) parent.getSelectedItem();
// //Toast.makeText(c, "city ID: "+city.getId()+", city Name : "+city.getName(), Toast.LENGTH_SHORT).show();
//// Intent intent = new Intent("locationSelection");
//// intent.putExtra("city",city.getId());
//// c.sendBroadcast(intent);
//
// String cityId=city.getId();
//
// }
//
// @Override
// public void onNothingSelected(AdapterView<?> parent) {
//
// }
// });
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void getData(){
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
JsonArrayRequest req = new JsonArrayRequest(urlAddress,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject result = (JSONObject) response
.get(i);
int id = result.getInt("id");
String name = result.getString("pro_name");
ProvinceList.add(new Province(String.valueOf(id), name));
}
} catch (JSONException e) {
e.printStackTrace();
}
//Log.i(TAG, String.valueOf(response));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, "Volley Error");
}
});
// Add the request to the RequestQueue.
queue.add(req);
}
private void getCityData(String urlAddress1){
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
JsonArrayRequest req = new JsonArrayRequest(urlAddress1,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject result = (JSONObject) response
.get(i);
int id = result.getInt("id");
String name = result.getString("city_name");
CityList.add(new City(String.valueOf(id), name));
}
} catch (JSONException e) {
e.printStackTrace();
}
//Log.i(TAG, String.valueOf(response));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, "Volley Error");
}
});
// Add the request to the RequestQueue.
queue.add(req);
}
答案 0 :(得分:1)
我遇到了同样的问题......我做的是......
我创建了一个单独的xml布局,其中包含textview,我使用了xml布局
ArrayAdapter adapter=new ArrayAdapter(RegisterActivity.this,R.layout.custom_drop_down_item,ProvinceList);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我的问题解决了!!
试试吧..可能它会帮助你......
答案 1 :(得分:1)
在RegisterActivity中添加此代码
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
CustomDropDownAdapter adapter = new CustomDropDownAdapter(this, list);
spinner.setAdapter(adapter);
adapter.setOnCustomItemSelectedListener(new CustomDropDownAdapter.OnCustomItemSelectedListener() {
@Override
public void onItemSelected(int position, String selectedValue) {
// this will hit if any item will be select
}
});
创建一个单独的类
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.kpsoft.xlez.reform.R;
import java.util.ArrayList;
public class CustomDropDownAdapter extends BaseAdapter {
private ArrayList<String> mList;
private Activity mContext;
private OnCustomItemSelectedListener mListener;
public CustomDropDownAdapter(Activity context, ArrayList<String> list){
this.mContext = context;
this.mList = list;
}
public void setOnCustomItemSelectedListener(OnCustomItemSelectedListener listener){
this.mListener = listener;
}
@Override
public int getCount() {
return mList == null ? 0 : mList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = mContext.getLayoutInflater();
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.item_drop_down, parent, false);
// well set up the ViewHolder
viewHolder.mTextView = (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mTextView.setText(mList.get(position));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mListener != null){
mListener.onItemSelected(position, viewHolder.mTextView.getText().toString());
}
}
});
return convertView;
}
private static class ViewHolder {
TextView mTextView;
}
public interface OnCustomItemSelectedListener{
void onItemSelected(int position, String selectedValue);
}
}
这是项目下拉列表的布局
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="18sp"
android:textColor="#000000"/>
这是完全有效的解决方案......我希望它能帮到你......