如何从Mysql中获取Spinner的选定数据并将其设置为Spinner

时间:2018-08-30 10:02:59

标签: android android-recyclerview android-spinner

我没有在Spinner中得到选定的Spinner项目,因为我已经为Spinner加载了静态数据,并且想在回收器视图中显示。我的代码在适配器中。 我想要Spinner中的“选定”数据,以便他们可以根据用户的选择再次对其进行编辑

package com.example.daniel_pc.contactsdirectory;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;

public class Display_Contact_adapter extends RecyclerView.Adapter<Display_Contact_adapter.ProductViewHolder> {
    ArrayList<Add_New_Contact_pojo> display_pojos;
    Context mcontext;
    Display_Contact_adapter madapter;
//    ArrayAdapter dataAdapter;
    ArrayAdapter<String> data_adapter = null;
    List<String> spinner_data = new ArrayList<String>();

    public Display_Contact_adapter(ArrayList<Add_New_Contact_pojo> display_pojos, Context mcontext) {
        this.display_pojos = display_pojos;
        this.mcontext = mcontext;
    }

    @Override
    public Display_Contact_adapter.ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = (LayoutInflater.from(parent.getContext()).inflate(R.layout.dynamic_add_delete_for_display,parent,false));
        Display_Contact_adapter.ProductViewHolder viewHolder = new Display_Contact_adapter.ProductViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final Display_Contact_adapter.ProductViewHolder holder, final int position) {
    final Add_New_Contact_pojo add_new_contact_pojo = display_pojos.get(position);
    madapter = new Display_Contact_adapter(display_pojos,mcontext);
    holder.id = add_new_contact_pojo.getId();
    holder.deletemultiplecontact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            removeItem(add_new_contact_pojo);
        }
    });
            data_adapter = new ArrayAdapter<String>((Activity)mcontext, android.R.layout.simple_spinner_item, spinner_data);
            data_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            holder.spinner_data_types.setAdapter(data_adapter);
            holder.ednumber.setText(add_new_contact_pojo.getText());
    }

    @Override
    public int getItemCount() {
        return display_pojos.size();
    }

    private void removeItem(Add_New_Contact_pojo ap) {
        int currentposition = display_pojos.indexOf(ap);
        display_pojos.remove(currentposition);
        notifyItemRemoved(currentposition);
    }

    public static class ProductViewHolder extends RecyclerView.ViewHolder {
        public int id;
        public Spinner spinner_data_types;
        public EditText ednumber;
        Button add,deletemultiplecontact;
        public ProductViewHolder(View itemView) {
            super(itemView);
            spinner_data_types = (Spinner) itemView.findViewById(R.id.display_type_spinner);
            ednumber = (EditText) itemView.findViewById(R.id.ednumberfordisplay);
            deletemultiplecontact = (Button) itemView.findViewById(R.id.btn_delete_for_display);

            spinner_data_types.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String item = parent.getItemAtPosition(position).toString();
//                    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }
    }
}

任何代码示例将不胜感激 预先感谢

0 个答案:

没有答案