在工厂中放置什么以创建Cardviews-RecyclerViews,适配器

时间:2018-09-25 09:12:55

标签: android android-recyclerview

我一直在按照本教程进行操作,并做了一些小调整以供自己使用:Android RecyclerView and CardView Tutorial

(我的应用程序的要点是当您按下晶圆厂时,会出现一个cardview。在每个cardview内是一个spinneredittexttextviewcheckbox。)

但是,我最终需要将一些代码添加到FAB中,以便添加cardview。但是我不确定如何使用适配器完成所有操作。教程要求我做productList = new ArrayList<>();,所以我可以使用productList.add,但我不知道该怎么做。

这是我主要活动代码的摘要:

FloatingActionButton floatingActionButton =(FloatingActionButton) findViewById(R.id.fab);

floatingActionButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        productList.add //what goes here???//
    }
});

这是我当前的create.java代码(上面的代码段位于其中)

public class create extends AppCompatActivity {

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

    //the recyclerview
    RecyclerView recyclerView;

    @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);

        Product.spinnerItemsList = 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<>();

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

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                productList.add //what goes here???//
            }
        });

        //creating recyclerview adapter
        ProductAdapter adapter = new ProductAdapter(this, productList);

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

    private class CSVFile {
        InputStream inputStream;

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

        public List<String> read() {
            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;
        }
    }
}

这是用于Spinner的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;
        }
    }
}

这是教程中的Product.Java

public class Product {

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

    public static List<String> spinnerItemsList = new ArrayList<String>();

    public Product(List spinner, String editText, Boolean checkBox, String textView5) {
        this.editText = editText;
        this.checkBox = checkBox;
        this.textView5 = textView5;
    }
    public String getEdittext () {
        return editText;
    }

    public boolean getCheckbox () {
        return checkBox;
    }

    public String getTextview () {
        return textView5;
    }
}

这也是教程中的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);
    }

    @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);
        }
    }
}

我完成了与Fyi教程相同的xml内容。

编辑

 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) {



                Product mProduct = new Product(List spinner, String editText, Boolean checkBox, String textView5);
                productList.add(mProduct); //what goes here???//
                if(adapter != null)
                    adapter.notifyDataSetChanged();
                //Handle the empty adapter here

            }
        });

layout_products.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp">


            <Spinner
                android:id="@+id/spinner"
                android:layout_width="159dp"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true" />

            <CheckBox
                android:id="@+id/checkBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentTop="true"
                android:layout_marginEnd="36dp" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginEnd="89dp"
                android:text="TextView" />

            <EditText
                android:id="@+id/editText"
                android:layout_width="34dp"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:ems="10"
                android:inputType="number" />
        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

使用以下更改产品型号

public class Product {

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

   // The following list is useless unless u don't use it somewhere so lets create getters and setters for this item
   private 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 String getEdittext () {
        return editText;
    }

    public boolean getCheckbox () {
        return checkBox;
    }

    public String getTextview () {
        return textView5;
    }
    public String getSpinnerItemsList () {
        return spinnerItemsList;
    }
}

好吧,我认为您只需要在数组列表中添加一个虚拟对象,然后只需调用notifyDataSetChanged()

//Step 1 reposition your adapter
        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) {
                //The problem is in the following line you need to pass the values but what you are doing is incorrect
                //Product mProduct = new Product(List spinner, String editText, Boolean checkBox, String textView5);
                //So replace the above line with the following code
            ArrayList<String> spinnerItem = new ArrayList<String>();
            spinnerItem.add("StackOverFlow");
                //There are better and efficient ways to achieve this, If you explain what do you want exaclty I might be able to help
                Product mProduct = new Product(spinnerItem, "EditText Value", true, "TextView Value");
                productList.add(mProduct); //what goes here???//
                if(adapter != null)
                  adapter.notifyDataSetChanged();
                else
                  //Handle the empty adapter here

            }
        });

我想现在有了上面的代码,您就可以开始了!

更新尝试在ProductAdapter.java中更改以下代码块

  @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
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String> (mCtx, android.R.layout.simple_spinner_item, Product.getSpinnerItemsList());
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerArrayAdapter); 
    }

答案 1 :(得分:2)

productList是产品列表。执行`productList.add()时,必须提供product类型的对象。

示例:

floatingActionButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Product p = new Product(...)
        productList.add(p);
        ProductAdapter adapter = new ProductAdapter(this, productList);
        //setting adapter to recyclerview
        recyclerView.setAdapter(adapter);
    }
});

p.s:Product的构造函数中的第一个参数无用,因为您没有在任何地方使用它。

有更好的方法来实现这一目标。