对于我的学校项目,我正在进行预算跟踪应用。我想将类别设置为具有自定义布局的微调器。
此布局包含ImageView和Text。我在sqlite中存储数据。但我无法实例化自定义微调适配器。在互联网上,每个样本都使用活动,但我正在使用片段。谢谢。
这是代码
这是片段
public class TrnAddFragment extends Fragment {
private Spinner categorySpn;
private Spinner subCategorySpn;
private Spinner sourceSpn;
private EditText trnDate; //Add date picker spinner later
private EditText trnAmount;
private Switch debitOrCreditSwc;
private TextView debitOrCreditStatus;
private EditText trnDesc;
private SpinnerAdapter adapter;
public TrnAddFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_trn_add, container, false);
categorySpn = (Spinner)view.findViewById(R.id.categorySpn);
subCategorySpn = (Spinner)view.findViewById(R.id.subCategorySpn);
sourceSpn = (Spinner)view.findViewById(R.id.sourceSpn);
trnDate = (EditText)view.findViewById(R.id.dateTxt);
trnAmount = (EditText)view.findViewById(R.id.amountTxt);
debitOrCreditSwc = (Switch)view.findViewById(R.id.debitOrCreditSwt);
debitOrCreditStatus = (TextView)view.findViewById(R.id.debitOrCreditTxt);
trnDesc = (EditText)view.findViewById(R.id.descTxt);
//debit&credit switch listener
debitOrCreditSwc.setChecked(true);
debitOrCreditSwc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
debitOrCreditStatus.setText("Debit");
}else {
debitOrCreditStatus.setText("Credit");
}
}
});
//debit&credit switch check&set before showing
if (debitOrCreditSwc.isChecked()){
debitOrCreditStatus.setText("Debit");
}else {
debitOrCreditStatus.setText("Credit");
}
//category spinner
DatabaseHelper dbHelper = new DatabaseHelper(getContext());
List<TrnCategory> categories = new ArrayList<>();
categories = dbHelper.getAllCategory();
// Resources passed to adapter to get image
Resources resources = getResources();
//CODE LINE IS RED IN HERE
adapter = new SpinnerAdapter(getContext(),R.layout.spinner_layout,categories,resources);
categorySpn.setAdapter(adapter);
//category spinner adapter
//ArrayAdapter<TrnCategory> adapter = new ArrayAdapter<TrnCategory>(getContext(),R.layout.spinner_layout,R.id.spn_category_txt,categories);
//categorySpn.setAdapter(adapter);
return view;
}
}
这是自定义微调器适配器
public class SpinnerAdapter extends ArrayAdapter<TrnCategory>{
private Activity activity;
private ArrayList<TrnCategory> data;
public Resources resources;
TrnCategory tempValues = null;
LayoutInflater inflater;
public SpinnerAdapter(TrnAddActivity activity, int resourceId, ArrayList data, Resources resources) {
super(activity,resourceId,data);
this.activity = activity;
this.data = data;
this.resources = resources;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return super.getDropDownView(position, convertView, parent);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
public View getCustomView (int position, View convertView, ViewGroup parent){
View view = inflater.inflate(R.layout.spinner_layout,parent,false);
tempValues = null;
tempValues = (TrnCategory)data.get(position);
TextView textView = (TextView)view.findViewById(R.id.spn_txt);
ImageView image = (ImageView)view.findViewById(R.id.spn_img);
if (position == 0){
textView.setText("Please select");
}else{
image.setImageResource(resources.getIdentifier("com.sabani.irfanyus.finalproject_1:mipmap/"
+ tempValues.getCategoryImageId(),null,null));
textView.setText(tempValues.getCategoryName());
}
return view;
}
}
这是微调器布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/spn_img"
android:layout_width="40dp"
android:layout_height="40dp"
/>
<TextView
android:id="@+id/spn_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default Text"
android:textSize="20dp"
android:padding="10dp"/>
</LinearLayout>
和实体类
public class TrnCategory {
private int id;
private int categoryId;
private String categoryName;
private String categoryImageId;
public TrnCategory() {
}
public TrnCategory(int categoryId, String categoryName, String categoryImageId) {
this.categoryId = categoryId;
this.categoryName = categoryName;
this.categoryImageId = categoryImageId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getCategoryImageId() {
return categoryImageId;
}
public void setCategoryImageId(String categoryImageId) {
this.categoryImageId = categoryImageId;
}
public String toString() {
return categoryName;
}
}