1

I followed a tutorial on how to link a csv file I have to android studio so I can view one row of the elements from the csv in a dropdown. (The left row is for 'number' and the right row is for 'items'.

but now I'm trying to figure out so that:

When the user selects an item from the dropdown, the corressponding 'number' (from the csv) shows up, on a TextView.

Is this possible? Any ideas on how to get this working?

Thanks!

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, (List<String>) 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;
        }

    }


}

create.java

public class create extends AppCompatActivity {


    private LinearLayout mLinearLayout;
    private ArrayList<SearchableSpinner> mSpinners;
    //TODO add the below list of buttons and checkboxes
    private List<AppCompatButton> mButtons = new ArrayList<>();
    private List<CheckBox> mCheckboxes = new ArrayList<>();
    private List<TextView> mTextviews = new ArrayList<>();
    private List<View> mViews = new ArrayList<>();
    //TODO I Added this to hold all the number to items values
    private Map<String, String> numberItemValues = new HashMap<>();
    //Button buttontest;
//TODO this is the item list variable I created as global
    List<String> itemList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        GlobalClass globalClass = (GlobalClass) this.getApplicationContext();


        ArrayList<String> items = new ArrayList<>();
        items.add(String.valueOf(mSpinners)); // add you selected item
        globalClass.setItems(items);


        mSpinners = new ArrayList<>();

        mLinearLayout = findViewById(R.id.my_linearLayout);

        //mLinearLayout.setBackgroundColor(Color.parseColor("#ffffff")); sets colour for whole mlinearLayout so if you want to pess it to elete it'll deelete deverhything .


        //mLinearLayout.addView(makeSpinner());    // First spinner


        //When user clicks the FAB button,  hide the existing layout, using View.GONE,and then create spinner and checkbox
        // programatically(refer to my aligning code) and then programatically set the mLinearLayou background to the bg.xml
        // layouts background = mLinearLayout
        //You can choose which all elements to hide, using their id

        //btn2.setVisibility(View.GONE);
        //take away the white button , .


        //code for the add button to add more items
        FloatingActionButton floatingActionButton =
                (FloatingActionButton) findViewById(R.id.fab);

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getBaseContext(), "Item added!", Toast.LENGTH_SHORT).show();


                // Handle ze click.
                final Spinner spinner = makeSpinner();
                mLinearLayout.addView(spinner);


                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) spinner.getLayoutParams();
                layoutParams.setMargins(5, 100, 10, 0); //top 70

                Resources resources = getResources();
                DisplayMetrics metrics = resources.getDisplayMetrics();

                layoutParams.height = (int) (70 * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)); //80
                layoutParams.width = (int) (240 * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)); //240
                spinner.setLayoutParams(layoutParams);

                final View newView = makeView();
                //TODO adds a new view that is suppose to replicate the button so when it is pressed, blah happens.
                //TODO i.e move the button code to the onclick View then delete button cause its useless.
                //Add a new view
                mLinearLayout.addView(newView);

                //TODO create new layout params here



                mViews.add(newView);

                final int listSize = mViews.size();


                //code for deleting the said item.
                newView.setOnClickListener(new View.OnClickListener() {
                    //start
                    @Override
                    public void onClick(View view) {

                        //when the 'new button' is pressed, alert shows if you are sure you want to delete the item or not.

                        final View.OnClickListener context = this;


                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create.this);


                        // set title
                        alertDialogBuilder.setTitle("Delete Item");

                        // set dialog message
                        alertDialogBuilder
                                .setMessage("Are you sure you want to delete this item?")
                                .setCancelable(false)
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        // if this button is clicked, close
                                        // current activity


                                        if (listSize > 0) {

                                            mCheckboxes.get(listSize - 1).setVisibility(View.GONE);
                                            mSpinners.get(listSize - 1).setVisibility(View.GONE);
                                            mViews.get(listSize - 1).setVisibility(View.GONE);
                                            mTextviews.get(listSize - 1).setVisibility(View.GONE);
                                            Toast.makeText(getBaseContext(), "Item removed.", Toast.LENGTH_SHORT).show();

                                        }


                                    }
                                })
                                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        // if this button is clicked, just close
                                        // the dialog box and do nothing
                                        dialog.cancel();
                                    }
                                });

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();


                    }
                });




                //Add a new checkbox
                final CheckBox newCheckbox = makeCheckbox();
                mLinearLayout.addView(newCheckbox);

                //TODO add checkbox to your list
                mCheckboxes.add(newCheckbox);



                final TextView newTextview = makeTextview();
                mLinearLayout.addView(newTextview);
                mTextviews.add(newTextview);

            }
        });


    }


    //DUPLICATING ITEMS WHEN FAB IS PRESSED//
    private CheckBox makeCheckbox() {
        //Create new Checkbox
        CheckBox checkbox = new CheckBox(this);

        // Setup layout
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        checkbox.setLayoutParams(layoutParams);
        return checkbox;
    }


    private TextView makeTextview() {
        //create new textview
        TextView textview = new TextView(this);

        //setup layout

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        textview.setLayoutParams(layoutParams);
        textview.setText("ihi");
        return textview;

    }



    private View makeView() {
        //create new View

        View view = new View(this);
        view.setBackgroundColor(Color.parseColor("#ffffff"));
        LinearLayout.LayoutParams layoutParams =  new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 100);
        new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, 50);
        //LinearLayout.LayoutParams.MATCH_PARENT,
        // LinearLayout.LayoutParams.WRAP_CONTENT);
        view.setClickable(true);




        view.setLayoutParams(layoutParams);


        //setup layout

        return view;


    }






    private Spinner makeSpinner() {
        //opens csv
        InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
        CSVFile csvFile = new CSVFile(inputStream);
        //TODO I made this variable global, declared it at the very top of this file
        itemList = csvFile.read();

        //Create new spinner
        // SearchableSpinner spinner = (SearchableSpinner) new Spinner(this, Spinner.MODE_DROPDOWN);
        SearchableSpinner spinner = new SearchableSpinner(this);


        // Setup layout
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        spinner.setLayoutParams(layoutParams);
        MyListAdapter adapter = new MyListAdapter(this, R.layout.listrow, R.id.txtid, itemList);


        spinner.setAdapter(adapter);

        //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 = items.get(position);
                 String aisleNumber = numberItemValues.get(currentItem);
                //TODO you can use the above aisle number to add to your text view
                 textview.setText(aisleNumber);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });


        //Add it to your list of spinners so you can retrieve their data when you click the getSpinner button
        mSpinners.add(spinner);
        return spinner;
    }



    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 hashmap 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;
        }
    }
}

CSV FILE:

Number,Item

0,Potato Chips

0,Eggs

0,Nuts

0,Popcorn

0,Serviettes

0,Muesli Bars

0,Party Items

1,Tea

1,Coffee

1,Drinking Chocolate

1,Milo

2,Energy Drinks

2,Water

2,Soft Drinks

2,Juice

3,Canned Fruit

3,Desserts

3,Spreads

3,Milk Products

4,Cereals

4,Biscuits

4,Crackers

5,Mexican

5,Canned Foods

5,Sauce

6,Pasta

6,International Foods

6,Cooking Sauce

6,Rice

6,Soup

6,Chocolate

7,Pickles

7,Relishes

7,Gravies

7,Sauces

7,Noodles

8,Flour

8,Sugar

8,Baking Needs

8,Food wrap

8,Salad Dressings

8,Herbs

8,Salts

8,Cooking Oil

9,Aircare

9,Household cleaner

10,Petware

11,Laundry

11,Dishwash

12,Baby needs

13,Skincare

14,Tissues

14,Shampoo

14,Conditioner

14,Health and beauty

14,Ice cream

14,Frozen goods

  • 2
    that depends on what you have done – Vladyslav Matviienko Aug 10 '18 at 05:26
  • what do you mean? –  Aug 10 '18 at 05:31
  • 2
    I mean that you have already implemented reading some values into your dropdown from CSV. So show how you did it – Vladyslav Matviienko Aug 10 '18 at 05:44
  • edited code in post. –  Aug 10 '18 at 05:50
  • I'm trying to put it in a text view –  Aug 10 '18 at 07:09
  • 1
    your csv is only 1 file? fab button trigger new element(checkbox, button, edittext & spinner) is that true? your csv should only read once`csvFile.read()` if only single file, store as global arraylist, spinner action listener set in your adapter, i think that's should be make sense, what do you think? – Iqbal Rizky Aug 13 '18 at 20:07
  • yes my csv is one file - I am a bit confused –  Aug 14 '18 at 04:26
  • hello? still confused –  Aug 15 '18 at 23:37
  • 1
    Is it possible to post the csv file? – Mr.O Aug 26 '18 at 07:44
  • Just copied and pasted it in. The spinner is already populated by the items on the right –  Aug 26 '18 at 07:46
  • But there are multiple options for each number in the list, for example if you choose "0" you have Eggs or Popcorn and other options, which one should be shown in TextView? Shouldn't each item have a unique number? @Magic_Whizz – Mr.O Aug 26 '18 at 08:04
  • the number represents aisle number, in a shop. so some items may have similar numbers. would that still work? –  Aug 26 '18 at 08:05
  • 1
    Sure we can make a work around, so the TextView will show multiple items then. @Magic_Whizz – Mr.O Aug 26 '18 at 08:12
  • Oh can't the textview just show the one number that corresponds to the item? Even if there are multiple items with the same number? Just show the number once? –  Aug 26 '18 at 08:13
  • The text view is showing the number not the item btw –  Aug 26 '18 at 08:13
  • I also need to add a TextView to thhat duplication process too probably –  Aug 26 '18 at 08:18
  • Just to clarify, when that FAB is pressed, along with the checkbox . spinner and view box, a textview will show. that's where the corresponding number will show up, once the user selects an item from dropdown –  Aug 26 '18 at 08:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178784/discussion-between-mr-o-and-magic-whizz). – Mr.O Aug 26 '18 at 08:32

3 Answers3

0

When the user selects an item from the dropdown, the corressponding 'number' (from the csv) shows up, on a TextView.

Is this possible? Any ideas on how to get this working?

Yes, it is possible. You have to do the following :

  • read the CSV file
  • create a data structure containing a map<item, number>
Community
  • 1
  • 1
Bruno
  • 3,872
  • 4
  • 20
  • 37
  • I already read items from the csv file for my dropdown , how can I implement the same thing for the textview? Can I do that with the same code I have currently? –  Aug 24 '18 at 02:54
  • What is the problem now ? You want to change the content of a TextView depending on the Spinner selection ? – Bruno Aug 24 '18 at 07:01
  • its just that I followed a tutorial to use a csv file to populate my spinner and was wondering if I could use somehow of that code to get the corresponding 'number' of the selected dropdown and put that into the textview –  Aug 24 '18 at 07:09
  • basically what you just said yes, in the first column of the csv file there are ittem names and on the second column there are numbers. so when you select an item from the dropdown the correponding number shows in a text view beside it –  Aug 24 '18 at 07:10
  • 1
    OK. You succeed to fill the dropdown with names of the first column, right ? To display the corresponding numbers, you have to build a `map` where key is the name and value is the number. Then, when you select a name from the dropdown, you're able to find the corresponding value in the map and to display it in a TextView – Bruno Aug 24 '18 at 07:14
  • where can I read more about building a `map` and implementing it in my code ?thank you –  Aug 24 '18 at 07:18
  • 1
    Well...[here for example](https://developer.android.com/reference/java/util/HashMap). See the [put() method](https://developer.android.com/reference/java/util/HashMap#put(K,%20V)) to fill your map and the [get() one](https://developer.android.com/reference/java/util/HashMap.html#get(java.lang.Object)) to retrieve data – Bruno Aug 24 '18 at 07:22
  • thank you for that. though I am still confused as to how to start this code. is there any real example on this that uses a csv as well –  Aug 25 '18 at 01:10
  • 1
    Are you asking us to do the job ? ;-) I'm sorry but I don't think to be more able than you do you the search... Try to implement it, don't panic and come back if questions – Bruno Aug 25 '18 at 09:06
  • thanks for all your help, I do appreciate it. So I added a `public V put (K key,V value) { }` but I'm unsure what goes in for V and k. You said K is the name, so by name you mean the selected item from the dropdown? and the value is the corresponding number right? How would I call for the number ... –  Aug 26 '18 at 00:08
  • I don't know the structure of your CSV file but I assume that you have two columns: name and number. When you read your file, you retrieve these information. Then, you have to put them into a `HashMap` to store them, with the `put()` method. First argument is the name and second is the corresponding value. I have no time today to develop more but I'll go back on Monday if you have still trouble – Bruno Aug 26 '18 at 07:26
0

You can create a HashMap<item, number> and store your values like this (Assuming both values are string values)

HashMap<String, String> resultList =new HashMap<String, String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
    String line;
    while ((line = reader.readLine()) != null) {
        String[] row = line.split(",");
        resultList.put(row[1],row[0]);
    }
} catch (IOException e) {
    Log.e("Main", e.getMessage());
} finally {
    try {
        inputStream.close();
    } catch (IOException e) {
        Log.e("Main", e.getMessage());
    }
}
return resultList;

This will return a HashMap

You can access the values using

for (Map.Entry<String,String> entry : resultList.entrySet()) {
    Toast.makeText("Item : " + entry.getKey() + " Number : " + entry.getValue()).show();
}

if you still want a List<String> of items you can create is using the above method. and you can get the corresponding 'number' to the item using the HashMap

String number = resultList.get(item);
NirmalCode
  • 2,140
  • 1
  • 14
  • 19
  • would this go in the mylistadapter class or create class? I assume create class? –  Aug 16 '18 at 19:31
  • Yes. In the create class. And if you want, you can store the HashMap in a global class . [Global Class](https://stackoverflow.com/a/51835451/5354229) . And use it from anywhere in your app – NirmalCode Aug 17 '18 at 03:56
  • I pretty much have that code already. Except without the hashmap. –  Aug 18 '18 at 02:13
  • 1
    Yes. I applied the `HashMap` to your posted code. You have used a `List` to store only items. To access the corresponding number you can use this `HashMap` method. And use a `Globalclass` to store that `HashMap` so that you can access it from any class or adapter in your code. – NirmalCode Aug 18 '18 at 04:51
  • I replace that but I get errors like unexpected token –  Aug 19 '18 at 04:22
  • hello? sorry still having issues –  Aug 20 '18 at 05:55
  • In the error log it just gave me a few errors such as `error: illegal start of type` `error: expected` `error: class, interface, or enum expected` `error: ';' expected` I only added the first block of code, not the second or the third yet. does that affect anything –  Aug 20 '18 at 07:14
  • Didn't it show you where is the error in blue color lines? – NirmalCode Aug 20 '18 at 07:16
  • No it didn't it just said `Compilation failed; see the compiler error output for details.` –  Aug 20 '18 at 07:16
  • hello? :) I appreciate your help –  Aug 26 '18 at 07:18
0

Your updated create.java Activity (I added TODOs to show the parts I changed):

Updated Code:

public class create extends AppCompatActivity {


private LinearLayout mLinearLayout;
private ArrayList<SearchableSpinner> mSpinners;
//TODO add the below list of buttons and checkboxes
private List<AppCompatButton> mButtons = new ArrayList<>();
private List<CheckBox> mCheckboxes = new ArrayList<>();
private List<TextView> mTextviews = new ArrayList<>();
private List<View> mViews = new ArrayList<>();
//TODO I Added this to hold all the number to items values
private Map<String, String> numberItemValues = new HashMap<>();
//Button buttontest;
//TODO this is the item list variable I created as global
List<String> itemList = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    GlobalClass globalClass = (GlobalClass) this.getApplicationContext();


    ArrayList<String> items = new ArrayList<>();
    items.add(String.valueOf(mSpinners)); // add you selected item
    globalClass.setItems(items);


    mSpinners = new ArrayList<>();

    mLinearLayout = findViewById(R.id.my_linearLayout);

    //mLinearLayout.setBackgroundColor(Color.parseColor("#ffffff")); sets colour for whole mlinearLayout so if you want to pess it to elete it'll deelete deverhything .


    //mLinearLayout.addView(makeSpinner());    // First spinner


    //When user clicks the FAB button,  hide the existing layout, using View.GONE,and then create spinner and checkbox
    // programatically(refer to my aligning code) and then programatically set the mLinearLayou background to the bg.xml
    // layouts background = mLinearLayout
    //You can choose which all elements to hide, using their id

    //btn2.setVisibility(View.GONE);
    //take away the white button , .


    //code for the add button to add more items
    FloatingActionButton floatingActionButton =
            (FloatingActionButton) findViewById(R.id.fab);

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getBaseContext(), "Item added!", Toast.LENGTH_SHORT).show();


            // Handle ze click.
            final Spinner spinner = makeSpinner();
            mLinearLayout.addView(spinner);


            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) spinner.getLayoutParams();
            layoutParams.setMargins(5, 100, 10, 0); //top 70

            Resources resources = getResources();
            DisplayMetrics metrics = resources.getDisplayMetrics();

            layoutParams.height = (int) (70 * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)); //80
            layoutParams.width = (int) (240 * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)); //240
            spinner.setLayoutParams(layoutParams);

            final View newView = makeView();
            //TODO adds a new view that is suppose to replicate the button so when it is pressed, blah happens.
            //TODO i.e move the button code to the onclick View then delete button cause its useless.
            //Add a new view
            mLinearLayout.addView(newView);

            //TODO create new layout params here



            mViews.add(newView);

            final int listSize = mViews.size();


            //code for deleting the said item.
            newView.setOnClickListener(new View.OnClickListener() {
                //start
                @Override
                public void onClick(View view) {

                    //when the 'new button' is pressed, alert shows if you are sure you want to delete the item or not.

                    final View.OnClickListener context = this;


                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create.this);


                    // set title
                    alertDialogBuilder.setTitle("Delete Item");

                    // set dialog message
                    alertDialogBuilder
                            .setMessage("Are you sure you want to delete this item?")
                            .setCancelable(false)
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // if this button is clicked, close
                                    // current activity


                                    if (listSize > 0) {

                                        mCheckboxes.get(listSize - 1).setVisibility(View.GONE);
                                        mSpinners.get(listSize - 1).setVisibility(View.GONE);
                                        mViews.get(listSize - 1).setVisibility(View.GONE);
                                        mTextviews.get(listSize - 1).setVisibility(View.GONE);
                                        Toast.makeText(getBaseContext(), "Item removed.", Toast.LENGTH_SHORT).show();

                                    }


                                }
                            })
                            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();


                }
            });




            //Add a new checkbox
            final CheckBox newCheckbox = makeCheckbox();
            mLinearLayout.addView(newCheckbox);

            //TODO add checkbox to your list
            mCheckboxes.add(newCheckbox);



            final TextView newTextview = makeTextview();
            mLinearLayout.addView(newTextview);
            mTextviews.add(newTextview);

            //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 = items.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);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
                }

            });


        }
    });


}


//DUPLICATING ITEMS WHEN FAB IS PRESSED//
private CheckBox makeCheckbox() {
    //Create new Checkbox
    CheckBox checkbox = new CheckBox(this);

    // Setup layout
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    checkbox.setLayoutParams(layoutParams);
    return checkbox;
}


private TextView makeTextview() {
    //create new textview
    TextView textview = new TextView(this);

    //setup layout

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    textview.setLayoutParams(layoutParams);
    textview.setText("ihi");
    return textview;

}



private View makeView() {
    //create new View

    View view = new View(this);
    view.setBackgroundColor(Color.parseColor("#ffffff"));
    LinearLayout.LayoutParams layoutParams =  new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 100);
    new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, 50);
    //LinearLayout.LayoutParams.MATCH_PARENT,
    // LinearLayout.LayoutParams.WRAP_CONTENT);
    view.setClickable(true);




    view.setLayoutParams(layoutParams);


    //setup layout

    return view;


}






private Spinner makeSpinner() {
    //opens csv
    InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
    CSVFile csvFile = new CSVFile(inputStream);
    //TODO I made this variable global, declared it at the very top of this file
    itemList = csvFile.read();

    //Create new spinner
    // SearchableSpinner spinner = (SearchableSpinner) new Spinner(this, Spinner.MODE_DROPDOWN);
    SearchableSpinner spinner = new SearchableSpinner(this);


    // Setup layout
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    spinner.setLayoutParams(layoutParams);
    MyListAdapter adapter = new MyListAdapter(this, R.layout.listrow, R.id.txtid, itemList);


    spinner.setAdapter(adapter);



    //Add it to your list of spinners so you can retrieve their data when you click the getSpinner button
    mSpinners.add(spinner);
    return spinner;
}



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 hashmap 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;
    }
}
}
Mr.O
  • 813
  • 7
  • 14