在recyclerview中向下滚动,微调器中的选定项目会更改

时间:2018-09-27 09:08:57

标签: android

当用户按下FAB时,卡片视图出现在recyclerview中。在cardview内部是一个带有其他元素的微调框。当我添加多个cardview并从微调器中选择一个项目时,然后我从fab添加另一个cardview,然后所有微调器选择的项目都重置了吗?

我有一个方法productList.size(),该方法可能有助于保存微调器选择的值,但我不知道从这里去哪里。谢谢

活动代码

public class create extends AppCompatActivity {



    //a list to store all the products
    List<Product> productList;

    //the recyclerview
    RecyclerView recyclerView;

    Product mProduct;
    private Map<String, String> numberItemValues = new HashMap<>();




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create);

        //opens csv
        InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
         CSVFile csvFile = new CSVFile(inputStream);

       final List<String>  mSpinnerItems = csvFile.read();


        //getting the recyclerview from xml
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //initializing the productlist
        productList = new ArrayList<>();




      final Spinner spinner;
      final EditText editText;
      final CheckBox checkBox;
      final TextView textView5;

      spinner = findViewById(R.id.spinner);
      editText = findViewById(R.id.editText);
      checkBox = findViewById(R.id.checkBox);
      textView5 = findViewById(R.id.textView5);

      final ProductAdapter  adapter = new ProductAdapter(this, productList);

        //TODO FAB BUTTON
        FloatingActionButton floatingActionButton =
                (FloatingActionButton) findViewById(R.id.fab);


        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Product> mProductList = new ArrayList<>();
                productList.add(mProduct);
                if(adapter != null)
                    adapter.notifyDataSetChanged();
                //Handle the empty adapter here

            }
        });


       /* //TODO Add the spinner on item selected listener to get selected items
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                String currentItem = mSpinnerItems.get(position);
                String aisleNumber = numberItemValues.get(currentItem);
                //TODO you can use the above aisle number to add to your text view
                //mTextviews.get(mTextviews.size() -1).setText(aisleNumber);
                textView5.setText(aisleNumber);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                //  do nothing
            }
        });*/






        //setting adapter to recyclerview
        recyclerView.setAdapter(adapter);






    }




    private class CSVFile {
        InputStream inputStream;

        public CSVFile(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        public List<String> read() {
// The problem with your adapter is solved now you need to do some work here its giving an error while reading the file
            //why is it giving an error because it use to work fine before I added cardview and recyclerview
            //Is the file there?
            List<String> resultList = new ArrayList<String>();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] row = line.split(",");
                    //TODO I edited this part so that you'd add the values in our new hash map variable

                    numberItemValues.put(row[1], row[0]);

                    resultList.add(row[1]);
                }
            } catch (IOException e) {
                Log.e("Main", e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    Log.e("Main", e.getMessage());
                }
            }
            return resultList;
        }
    }



}

product.java代码

public class Product {

    private static String editText;
    private static Boolean checkBox;
    private static String textView5;

    // The following list is useless unless u don't use it somewhere so lets create getters and setters for this item
    public static List<String> spinnerItemsList = new ArrayList<String>();

    public Product(List spinner, String editText, Boolean checkBox, String textView5) {

        this.editText = editText;
        this.spinnerItemsList = spinner;
        this.checkBox = checkBox;
        this.textView5 = textView5;
    }
    public static String getEdittext () {
        return editText;
    }

    public static boolean getCheckbox () {
        return checkBox;
    }

    public static String getTextview () {
        return textView5;
    }
    public static List<String> getSpinnerItemsList () {
        return spinnerItemsList;
    }
}

productadapter.java

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {


    //this context we will use to inflate the layout
    private Context mCtx;
    private Spinner spinner;

    //we are storing all the products in a list
    private List<Product> productList;

    //getting the context and product list with constructor
    public ProductAdapter(Context mCtx, List<Product> productList) {
        this.mCtx = mCtx;
        this.productList = productList;
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflating and returning our view holder
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.layout_products, null);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ProductViewHolder holder, int position) {
        //getting the product of the specified position
        Product product = productList.get(position);

        //binding the data with the viewholder views


        //MyListAdapter adapter = new MyListAdapter(mCtx, R.layout.listrow, R.id.txtid, Product.spinnerItemsList);
        //spinner.setAdapter(adapter);
        //Try using Simple ArrayAdapter if all you need to display is one text
        //Lets see
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String> (mCtx, android.R.layout.simple_spinner_item, Product.getSpinnerItemsList());
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       //Did it work?
        //yes but when I press the spinner I do not see the items that usually comes up also the layout of Oit Oipen the screen let me take the screen shot
        //how do i do that? the screen is open on my phone ok wait click the spinner yes
        //There is no item??
        //nothing comes up. not even a dropdown. but before i had it like it was populated by items in a csv.
        //Okay wait let me get your old code

        //also, I added cardviews in my phone but i try to scroll up and I cannot? i thought you could scroll in recyclerview?
        //Yes you can scroll it should work

        holder.spinner.setAdapter(spinnerArrayAdapter);
    }


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



    class ProductViewHolder extends RecyclerView.ViewHolder {

        Spinner spinner;
        EditText editText;
        TextView textView5;
        CheckBox checkBox;



        public ProductViewHolder(View itemView) {
            super(itemView);

            spinner = itemView.findViewById(R.id.spinner);
            editText = itemView.findViewById(R.id.editText);
            textView5 = itemView.findViewById(R.id.textView5);
            checkBox = itemView.findViewById(R.id.checkBox);
        }
    }
}

** mylistadapter.java,适用于微调器**

public class MyListAdapter extends ArrayAdapter<String> {
    int groupid;
    List<String> items;
    Context context;
    String path;





    public MyListAdapter(Context context, int vg, int id, List<String> items) {
        super(context, vg, id, items);
        this.context = context;
        groupid = vg;
        this.items = items;

    }

    static class ViewHolder {
        public TextView textid;
        public TextView textname;

    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        {

            View rowView = convertView;
            if (rowView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                rowView = inflater.inflate(groupid, parent, false);
                ViewHolder viewHolder = new ViewHolder();
                viewHolder.textid = (TextView) rowView.findViewById(R.id.txtid);
                viewHolder.textname = (TextView) rowView.findViewById(R.id.txtname);
                rowView.setTag(viewHolder);
            }
            // Fill data in the drop down.
            ViewHolder holder = (ViewHolder) rowView.getTag();
            String row = items.get(position);
            //holder.textid.setText(row[0]); //prints aisle number, dont need

            holder.textname.setText(row);



            return rowView;
        }

    }

}

1 个答案:

答案 0 :(得分:1)

有多个步骤可以实现这一目标:

第1步:

Map<Integer, Integer> mSpinnerSelectedItem = new HashMap<Integer, Integer>();

第2步:

onBindViewHolder内添加以下行

if (mSpinnerSelectedItem.containsKey(position)) {
   spinner.setSelection(mSpinnerSelectedItem.get(position));
} 

第3步:

现在,在您设置微调器onItemSelected方法的位置,添加以下行来存储该微调器的所选项目。

mSpinnerSelectedItem.put(position, selectedIndex);