我一直试图自己实现一个多选微调器。所以问题是我能够选中复选框,但当我关闭并再次打开微调器时,所选的复选框将被取消选中。所以我想我会将自己的itemclick监听器实现到微调器并使用位置来检查复选框。现在,我的旋转器没有掉下来。
这是主要活动的代码。
public class MyProfileActivity extends Activity implements
View.OnClickListener, SpinnerMultiSelectAdapter.OnListItemClickListener{
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
lang_spinner_profile = findViewById(R.id.lang_spinner_profile);
ArrayList<String> options = new ArrayList<>();
options.add("English");
options.add("Japanese");
options.add("Chinese");
options.add("Korean");
lang_spinner_profile.setSelection(0);
final String[] languages = {
"Select Language", "English","Chinese","Japanese","Korean"};
for (int j = 0; j < languages.length; j++) {
StateVO stateVO= new StateVO();
stateVO.setTitle(languages[j]);
stateVO.setSelected(false);
listVOs.add(stateVO);
}
Log.d("Called","Called");
myAdapter = new SpinnerMultiSelectAdapter(MyProfileActivity.this, 0,
listVOs, this);
lang_spinner_profile.setAdapter(myAdapter);
}
@Override
public void onListItemClick(int position) {
Toast.makeText(this, "Clicked at: "+position, Toast.LENGTH_SHORT).show();
}
}
这是我为微调器设计的自定义pojo类。
public class StateVO {
private String title;
private boolean selected;
private String selectedItem;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
这是微调器自定义布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="text"
android:textAlignment="gravity" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
最后,这是我的自定义适配器。
public class SpinnerMultiSelectAdapter extends ArrayAdapter<StateVO> {
private Context mContext;
static private ArrayList<StateVO> listState;
private SpinnerMultiSelectAdapter myAdapter;
private boolean isFromView = false;
List<String> selected = new ArrayList<>();
static int i=0;
List<Integer> checked= new ArrayList<>();
View itemView;
int getPosition;
private OnListItemClickListener onListItemClickListener;
public SpinnerMultiSelectAdapter(Context context, int resource, List<StateVO> objects, OnListItemClickListener onListItemClickListener) {
super(context, resource, objects);
this.mContext = context;
this.listState = (ArrayList<StateVO>) objects;
this.myAdapter = this;
this.onListItemClickListener = onListItemClickListener;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item, null);
itemView = convertView;
holder = new ViewHolder();
holder.mTextView = (TextView) convertView
.findViewById(R.id.text);
holder.mCheckBox = (CheckBox) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
// To check weather checked event fire from getview() or user input
isFromView = true;
holder.mCheckBox.setChecked(listState.get(position).isSelected());
isFromView = false;
if ((position == 0)) {
holder.mCheckBox.setVisibility(View.INVISIBLE);
} else {
holder.mCheckBox.setVisibility(View.VISIBLE);
}
holder.mCheckBox.setTag(position);
selected.clear();
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
getPosition = (Integer) buttonView.getTag();
holder.mCheckBox.setSelected(true);
//notifyDataSetChanged();
}
});
return convertView;
}
private class ViewHolder implements View.OnClickListener {
private TextView mTextView;
private CheckBox mCheckBox;
public ViewHolder(){
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
onListItemClickListener.onListItemClick(getPosition);
}
}
public interface OnListItemClickListener {
public void onListItemClick(int position);
}
}
感谢任何帮助。谢谢。
答案 0 :(得分:1)
你的适配器几乎没有错误,应该如何;
public ParseResponse getParseResponseObj(ParseRequest req){
this.parseResponse = getParseResponse(req);
return this.parseResponse;
}
private ParseResponse getParseResponse(ParseRequest request){
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
final String BASE_URL = "https://api.infermedica.com/v2/";
OkHttpClient.Builder httpClient =
new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit.Builder builder =
new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build());
Retrofit retrofit = builder.client(httpClient.build()).build();
InfermedicaClient infermedicaClient = retrofit.create(InfermedicaClient.class);
//final MutableLiveData<ParseResponse> resp = new MutableLiveData<>();
//parseResponse = new ParseResponse();
Call<ParseResponse> call = infermedicaClient.parseUserInput(Constants.APP_ID,Constants.APP_KEY,request);
call.enqueue(new Callback<ParseResponse>() {
@Override
public void onResponse(Call<ParseResponse> call, Response<ParseResponse> response) {
if (response.isSuccessful()) {
parseResponse = response.body();
}
Log.v("ParseReq Success" , response.message());
}
@Override
public void onFailure(Call<ParseResponse> call, Throwable t) {
Log.v("ParseReq Success" , t.getMessage());
}
});
return parseResponse;
}