So, I do have a MainActivity were inside there is a spinner
Java code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final boolean[] selected_action = {false, false, false};
final String[] select_action = {"Select the option(s)",
"WhatsApp backup databases", "Images Miniature", "Caches"};
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList<StateVO> listVOs = new ArrayList<>();
for (String aSelect_action : select_action) {
StateVO stateVO = new StateVO();
stateVO.setTitle(aSelect_action);
stateVO.setSelected(stateVO.isSelected());
listVOs.add(stateVO);
}
MyAdapter myAdapter = new MyAdapter(MainActivity.this, 0, listVOs);
spinner.setAdapter(myAdapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for (int i = 0; i < selected_action.length; i++) {
if (selected_action[i]) {
Toast.makeText(MainActivity.this, "True", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
And here it is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lorenzomoscati.remover.MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="164dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="61dp"
android:text="@string/start" />
</RelativeLayout>
Cool, then I've made a StateVO.java for the spinner as well as a custom adapter called MyAdapter.java.
StateVO.java:
public class StateVO {
private String title;
private boolean selected;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
boolean isSelected() {
return selected;
}
void setSelected(boolean selected) {
this.selected = selected;
}
}
MyAdapter.java:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.CompoundButton;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends ArrayAdapter<StateVO> {
private Context mContext;
private ArrayList<StateVO> listState;
private MyAdapter myAdapter;
private boolean isFromView = false;
public MyAdapter(Context context, int resource, List<StateVO> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = (ArrayList<StateVO>) objects;
this.myAdapter = this;
}
@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 layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.spinner_item, null);
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());
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);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
}
});
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private CheckBox mCheckBox;
}
}
So the problem is the following: How do I get checked if the checkboxes inside the spinner are checked or nor? In which file do I have to put the checking code? What should be like the checking code?
I've tried a lot (OnClick for example), but I didn't get so far. Probably because I implemented the code in a wrong way. I'd be glad if anyone can help.
Aucun commentaire:
Enregistrer un commentaire