Spinner in ListView



  • I have ListViewItem ListView There's a falling list.SpinnerThere's also a "+" button by which the user can add new ones. Items Total ListView. Let's say the laser created two. Items and selected values Spinnerwhen adding Itemvalues selected in the first two Items Total Spinner I can't figure out how to keep things right and then picture them when creating new ones. Items

    It was an idea to keep it. ArrayList at onItemSelected in the range of values chosen, but then it doesn't make sense what to do if the laser first chose one meaning and then changed it to another. Or maybe you should use it. Expandable ListView?

    Code in Activity:

    public class ClaimWorkActivity extends Activity {
    ArrayList <String> workCount = new ArrayList<>();
    WorksAdapter worksAdapter;
    

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.claim_work_activity);
    workCount.add("1");
    lvListWork = (ListView) findViewById(R.id.lvListWork);
    btnAdd =(Button) findViewById(R.id.btnAdd);
    worksAdapter = new WorksAdapter(this);
    lvListWork.setAdapter(worksAdapter);

    btnAdd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    workCount.add("1");
    worksAdapter.addall(workCount);
    }
    });
    worksAdapter.addall(workCount);
    }
    }
    }

    Adapter code:

    public class WorksAdapter extends BaseAdapter {

    Context ctx;
    LayoutInflater lInflater;
    ArrayList&lt;String&gt; objects;
    
    private String [] workType = {"Work type 1", "Work type 2", "Work type 3", "Work type 4", "Work type 5"};
    
    
    public WorksAdapter(Context context) {
        ctx = context;
        objects=new ArrayList&lt;&gt;();
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getCount() {
        return objects.size();
    }
    
    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    public void addall(ArrayList&lt;String&gt; object){
        objects.clear();
        objects.addAll(object);
        notifyDataSetChanged();
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.claim_work_item, parent, false);
        }
        ((TextView) view.findViewById(R.id.tvWorkNumber)).setText(String.valueOf(position+1));
        //objects.get(position)
        Spinner spinnerClaimWorkName= (Spinner) view.findViewById(R.id.tvClaimWorkName);
    
        ArrayAdapter&lt;String&gt; adapterWorkType = new ArrayAdapter&lt;String&gt;(ctx, android.R.layout.simple_spinner_item, workType);
        adapterWorkType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
        spinnerClaimWorkName.setAdapter(adapterWorkType);
        spinnerClaimWorkName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView&lt;?&gt; parent, View view,
                                       int position, long id) {
                Log.d("Work Type", workType[position]);
            }
    
            @Override
            public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
            }
        });
        return view;
    }
    

    }



  • I've decided to redo it. ListView c SpinnerExpandableListViewI think that in this situation, he's better suited, at least that's why we're gonna need to use one adapter. I did. OnClickListenerExpandableListView and every time a laser chooses a position child♪ I'm assigning this value to the list on which to build ♪ headers I'm updating everything. ExpandableListView♪ I also have a button. btnAdd by which a laser can add new Items, add a new element to the mass on which headers are built and updated ExpandableListView

    Activity code:

    public class MainActivity extends Activity{
    ClaimExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    private Button btnAdd;
    List<String> listDataHeader;
        HashMap<String, List<String>> listDataChild;
        List<String> docTypes;
    

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.claim_work_activity);
    expListView = (ExpandableListView) findViewById(R.id.elvMain);
    btnAdd =(Button) findViewById(R.id.btnAdd);

    prepareListData();
    listAdapter = new ClaimExpandableListAdapter(this, listDataHeader, listDataChild);
    expListView.setAdapter(listAdapter);
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                                            int groupPosition, int childPosition, long id) {
    
                    listDataHeader.set(groupPosition, listDataChild.get(
                            listDataHeader.get(groupPosition)).get(
                            childPosition));
                    listDataChild.put(listDataHeader.get(groupPosition), docTypes);
                    listAdapter.updateData(listDataHeader,listDataChild);
                    listAdapter.notifyDataSetChanged();
    
                    return false;
                }
            });
        }
    

    btnAdd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    listDataHeader.add("Выберите тип работ");
    listDataChild.put(listDataHeader.get(listDataHeader.size()-1), docTypes);
    listAdapter.updateData(listDataHeader,listDataChild);
    listAdapter.notifyDataSetChanged();
    }
    });
    }

    private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Выберите тип работ");
    
        // Adding child data
        docTypes = new ArrayList&lt;String&gt;();
        docTypes.add("Work Type 1");
        docTypes.add("Work Type 2");
        docTypes.add("Work Type 3");
        docTypes.add("Work Type 4");
        docTypes.add("Work Type 5");
        listDataChild.put(listDataHeader.get(0), docTypes); 
    }
    

    }

    Adapter code:

    public class ClaimExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    public List&lt;String&gt; _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap&lt;String, List&lt;String&gt;&gt; _listDataChild;
    
    public ClaimExpandableListAdapter(Context context, List&lt;String&gt; listDataHeader,
                                      HashMap&lt;String, List&lt;String&gt;&gt; listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
    
    public ClaimExpandableListAdapter(Context context, List&lt;String&gt; listDataHeader) {
        this._context = context;
        this._listDataHeader = listDataHeader;
    }
    
    
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
    
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
    
        final String childText = (String) getChild(groupPosition, childPosition);
    
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.claim_explist_item, null);
        }
        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);
    
        txtListChild.setText(childText);
        return convertView;
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }
    
    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }
    
    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }
    
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.claim_list_group, null);
        }
    
        TextView tvWorkNumber = (TextView) convertView
                .findViewById(R.id.tvWorkNumber);
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.tvClaimWorkName);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        int displayPosition=groupPosition+1;
        tvWorkNumber.setText(String.valueOf(displayPosition));
    
        return convertView;
    }
    
    @Override
    public boolean hasStableIds() {
        return false;
    }
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    
    
    public void updateData(List&lt;String&gt; groups,
                           HashMap&lt;String, List&lt;String&gt;&gt; children) {
        this._listDataHeader = groups;
        this._listDataChild = children;
    }
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2