アダプタを更新していない私は、カスタムと1
CheckBox
としています。私はアダプタを介してスピナーにアイテムを追加しています。チェックボックスをもう一度チェックすると、チェックが外されるか、またはチェックされたポジションに変更が加えられました。スピナーチェックボックスボックスの状態が偽の取得と私はスピナーで選択した項目が
EditText
に表示する必要があり、これまで何の両方Array
からすなわちproduct_30またはproduct_80- から来るデータから2
Array
を持っています。
コード
配列:
<string-array name="product_2_30">
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
<item>E</item>
<item>N</item>
</string-array>
<string-array name="product_2_80">
<item>A1</item>
<item>B2</item>
<item>C3</item>
<item>D4</item>
<item>F5</item>
<item>NN</item>
</string-array>
activity_main.xml
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.pankajkumar.adaptertest.MainActivity">
<RadioGroup
android:id="@+id/rgGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/item_2_30"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="30" />
<RadioButton
android:id="@+id/item_2_80"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="80" />
</RadioGroup>
<LinearLayout
android:id="@+id/spinerlayout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/rgGroup"
android:background="@drawable/insect_test"
android:orientation="horizontal">
<Spinner
android:id="@+id/myspinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="4"
android:background="@android:color/transparent"
android:gravity="center"
android:spinnerMode="dropdown" />
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:onClick="showDropDown"
android:paddingRight="5dp"
android:src="@drawable/drop_down_arrow" />
</LinearLayout>
<EditText
android:id="@+id/edt_itemList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/spinerlayout"
android:layout_marginTop="@dimen/activity_horizontal_margin" />
</RelativeLayout>
MainActivity.java
あなたはスピナー項目ではないリスト位置のための位置を取得しているgetDropDownView方法でpublic class MainActivity extends AppCompatActivity {
private String[] item_2_30, item_2_80;
private Spinner mSpinner;
private MyBaseAdapter adapter_30, adapter_80;
public static ArrayList<Modal> modals_2_30, modals_2_80;
private RadioGroup group;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.myspinner);
item_2_30 = getResources().getStringArray(R.array.product_2_30);
item_2_80 = getResources().getStringArray(R.array.product_2_80);
group = (RadioGroup) findViewById(R.id.rgGroup);
editText = (EditText) findViewById(R.id.edt_itemList);
modals_2_30 = AddItemToSpinner.addItem(item_2_30);
modals_2_80 = AddItemToSpinner.addItem(item_2_80);
adapter_30 = new MyBaseAdapter(this, modals_2_30, editText);
adapter_80 = new MyBaseAdapter(this, modals_2_80, editText);
mSpinner.setAdapter(adapter_30);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.item_2_30:
mSpinner.setAdapter(adapter_30);
break;
case R.id.item_2_80:
mSpinner.setAdapter(adapter_80);
break;
}
}
});
}
public void showDropDown(View view) {
mSpinner.performClick();
}
}
MyBaseAdapter.java
public class MyBaseAdapter extends BaseAdapter implements SpinnerAdapter {
private static final String TAG = "MyBaseAdapter";
private Context mContext;
private ArrayList<Modal> modals;
private LayoutInflater inflater;
private ViewHolder holder;
private EditText editText;
private int count = 1;
private int po;
public MyBaseAdapter(MainActivity mContext, ArrayList<Modal> modals, EditText editText) {
this.mContext = mContext;
this.modals = modals;
this.editText = editText;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
Log.e(TAG, "unregisterDataSetObserver: ");
addSelectedItem(MainActivity.modals_2_30);
addSelectedItem(MainActivity.modals_2_80);
}
@Override
public View getDropDownView(int position, View view, ViewGroup parent) {
po = position;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.spinner_row, parent, false);
holder.txt_item_name = (TextView) view.findViewById(R.id.txt_item_name);
holder.chk_check_item = (CheckBox) view.findViewById(R.id.chk_item_check);
view.setTag(holder);
view.setTag(R.id.txt_item_name, holder.txt_item_name);
view.setTag(R.id.chk_item_check, holder.chk_check_item);
} else {
holder = (ViewHolder) view.getTag();
}
holder.txt_item_name.setText(modals.get(position).getListItem());
holder.chk_check_item.setChecked(modals.get(position).isCheck());
holder.chk_check_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
modals.get(po).setCheck(b);
}
});
holder.txt_item_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//int getposition1 = (Integer) view.getTag();
if (modals.get(po).isCheck()) {
modals.get(po).setCheck(false);
} else {
modals.get(po).setCheck(true);
}
}
});
// notifyDataSetChanged();
return view;
}
@Override
public int getCount() {
return modals.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
po = position;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.spinner_row, viewGroup, false);
holder.txt_item_name = (TextView) view.findViewById(R.id.txt_item_name);
holder.chk_check_item = (CheckBox) view.findViewById(R.id.chk_item_check);
view.setTag(holder);
view.setTag(R.id.txt_item_name, holder.txt_item_name);
view.setTag(R.id.chk_item_check, holder.chk_check_item);
} else {
holder = (ViewHolder) view.getTag();
}
holder.txt_item_name.setText(modals.get(position).getListItem());
holder.chk_check_item.setChecked(modals.get(position).isCheck());
// holder.chk_check_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// modals.get(po).setCheck(b);
// }
// });
//
// holder.txt_item_name.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// //int getposition1 = (Integer) view.getTag();
// if (modals.get(po).isCheck()) {
// modals.get(po).setCheck(false);
// } else {
// modals.get(po).setCheck(true);
// }
//
// }
// });
// notifyDataSetChanged();
return view;
}
class ViewHolder {
TextView txt_item_name;
CheckBox chk_check_item;
}
public void addSelectedItem(ArrayList<Modal> modals) {
for (Modal m : modals) {
if (m.isCheck()) {
editText.setText(m.getListItem() + "\n");
}
}
}
}
私は元の位置を得ることができる方法。最後のインデックスを取得していますか? –
どのようにデータをスピナーに設定していますか? –
に記載されている。 –