可扩展列表视图列表Appasres根据数字选择器中的数字项选择

时间:2017-03-02 09:09:40

标签: android

这里我有数字选择器我想在数字选择器中选择数字然后在可扩展列表视图数据将根据数字选择器中选择的数字显示。请帮帮我。When i select the number 1 then child 1 appears only and when i select the number 2 in number picker then child 1 and child 2 only Display on screen     这里是我的MainJava类     package com.example.elite_android.explistview1;

import android.content.ClipData;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.NumberPicker;

public class MainActivity extends AppCompatActivity {
    ExpandableListView expandableListView;

    private ExpandableListAdapter mAdapter;
    ExpandableListView expand;
    NumberPicker numberPicker;

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




        String [] numbers = new String[10];
        for (int count = 0; count < 10; count++)
            numbers[count] = String.valueOf(count);

        numberPicker = (NumberPicker) findViewById(R.id.main_number_picker);
        numberPicker.setMaxValue(numbers.length);
        numberPicker.setMinValue(1);
        numberPicker.setDisplayedValues(numbers);

        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            }
        });


        expandableListView = (ExpandableListView) findViewById(R.id.exp_Listview);

        List<String> headerItems = new ArrayList<String>();
        headerItems.add("Child 1");
        headerItems.add("Child 2");
        headerItems.add("Child 3");

        ArrayList childDetails = new ArrayList();
        childDetails.add("Name");
        childDetails.add("Age");
        childDetails.add("Gender");

        HashMap<String, List<String>> childList = new HashMap<String, List<String>>();
        childList.put(headerItems.get(0), childDetails);
        childList.put(headerItems.get(1), childDetails);
        childList.put(headerItems.get(2), childDetails);

        mAdapter = new MyAdapter(this, headerItems, childList);
        expandableListView.setAdapter(mAdapter);
    };



        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu, menu);
            return true;
        }
    }

`
**and here my Adapter class**

    package com.example.elite_android.explistview1;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.NumberPicker;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ELITE-ANDROID on 28-02-2017.
 */
// this is provides all needs methods  implementing an Expandable List VIew

public class MyAdapter extends BaseExpandableListAdapter {

    // We need some Variables here so therer are some Variables here

     private List<String> header_titles;                     // This is for Representing the HeadLine
     private HashMap<String, List<String>> child_titles;     //  defin the HashMap for the child item how to Represent the Parent Handing so Hash Map, need some Variables
    private Context ctx;
    private NumberPicker numberPicker;


    //for  initalized for all Variables we need some Constructor
    public MyAdapter(Context context,List<String>header_titles,HashMap<String,List<String>>child_titles) {

        this.ctx = context;
        this.child_titles = child_titles;
        this.header_titles = header_titles;
    }


    @Override
    // From this Override  method How to Return how many elements are the Group count like parent
    public int getGroupCount()
    {
        return header_titles.size();
    }


    @Override
    //here  the number of child items are in each heading. There are three Heading - PAtien Name ,Age, Gender
    public int getChildrenCount(int groupPosition) {

//        Log.d("xxx", )
        return child_titles.get(header_titles.get(groupPosition)).size(); //how to Return the size of HashMap

//        return child_titles.get(header_titles.get(groupPosition)).size();
    }


    @Override
    public Object getGroup(int groupPosition)
    {
        return header_titles.get(groupPosition);
    }

    @Override
    // here how to retuen child items on the particular headings and Positions.
    public Object getChild(int groupPosition, int childPosition) {
        return child_titles.get(header_titles.get(groupPosition)).get(childPosition);
  }


    @Override

    //return the groupo position
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    @Override
    // Here return the Group View
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {

//        header_titles = new ArrayList<String>();
//        header_titles.add("List 1");

        // Here how to get the Heading Title here Decalered String Variables, now how to get title of heading from getGroup methods so simple call Backed Methods.
        String title = (String)this.getGroup(groupPosition);
        if (convertView == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.prent,null);
        }

        TextView textView = (TextView) convertView.findViewById(R.id.heading_item);
        textView.setText(title);     //  for Heading bold style
        textView.setText(title);     // for set the title

        return convertView;
    }


    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,  View convertView, ViewGroup parent) {
        String title = (String) this.getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child, null);
        }

        TextView sequence = (TextView) convertView.findViewById(R.id.sequence);
        //sequence.setText();
        //sequence.setText(detailInfo.getSequence().trim() + ") ");
        TextView childItem = (TextView) convertView.findViewById(R.id.child_item);
        childItem.setText(title);

        return convertView;
    }



    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }
    }

1 个答案:

答案 0 :(得分:0)

    package com.example.elite_android.explistview1;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.NumberPicker;

public class MainActivity extends AppCompatActivity {
    ExpandableListView expandableListView;

    private MyAdapter mAdapter;
    ExpandableListView expand;
    NumberPicker numberPicker;

    private List<String> headerItems;
    private ArrayList childDetails;
    private HashMap<String, List<String>> childList;
    private Context ctx;

    EditText editText1;

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

//        get reference to the EditText
        editText1 = (EditText) findViewById(R.id.child_item);
//        //set the onFocusChange listener
//        // editText1.setOnFocusChangeListener(editText1.getOnFocusChangeListener());
//
////        //get reference to EditText
////        editText2 = (EditText) findViewById(R.id.sequence);
////        // set the on focusChange listner
////        editText2.setOnFocusChangeListener(editText2.getOnFocusChangeListener());
//
//        //Generate list View from ArrayList;
//
//
//        EditText editText = (EditText) findViewById(R.id.child_item);
//        editText.requestFocus();
//        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
//
//
////        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//        //          @Override
//        //        public void onFocusChange(View v, boolean hasFocus) {
//        //          //editText.setOnFocusChangeListener();
//        //        return;
//        //  }
//        //});
//

        String[] numbers = new String[10];
        for (int count = 0; count < 10; count++)
            numbers[count] = String.valueOf(count);

        numberPicker = (NumberPicker) findViewById(R.id.main_number_picker);
        numberPicker.setMaxValue(numbers.length);
        numberPicker.setMinValue(1);
        numberPicker.setDisplayedValues(numbers);

        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                generateListItems(newVal);
                loadRecycleView();


            }
        });
    }

    private void generateListItems(int childCount) {
        if (headerItems != null)
            headerItems.clear();
        else
            headerItems = new ArrayList<>();

        if (childList != null)
            childList.clear();
        else
            childList = new HashMap();

        if (childDetails == null) {
            childDetails = new ArrayList();
            childDetails.add("Name");
            childDetails.add("Age");
            childDetails.add("Gender");
        }

        // Put header items
        for (int count = 0; count < childCount; count++) {
            headerItems.add(" Parent " + count);
            childList.put(headerItems.get(count), childDetails);
        }
    }

    private void loadRecycleView() {
        if (expandableListView == null) {
            expandableListView = (ExpandableListView) findViewById(R.id.exp_Listview);
            mAdapter = new MyAdapter(this, headerItems, childList);
            expandableListView.setAdapter(mAdapter);
        } else {
            expandableListView.invalidate();
            mAdapter.setHeaderData(headerItems);
            mAdapter.setListData(childList);
            mAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

}


**Adapter class**


    package com.example.elite_android.explistview1;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ELITE-ANDROID on 28-02-2017.
 */
// this is provides all needs methods  implementing an Expandable List VIew

public class MyAdapter extends BaseExpandableListAdapter {

    // We need some Variables here so therer are some Variables here


    private List<String> header_titles;                     // This is for Representing the HeadLine(Array list)
    private HashMap<String, List<String>> child_titles;     //  defin the HashMap for the child item how to Represent the Parent Handing so Hash Map, need some Variables
    private Context ctx;
    private NumberPicker numberPicker;
    EditText editText;
    private static LayoutInflater inflater = null;

    //for  initalized for all Variables we need some Constructor
    public MyAdapter(Context context, List<String> header_titles, HashMap<String, List<String>> child_titles) {

        super();
        this.ctx = context;
        this.child_titles = child_titles;
        this.header_titles = header_titles;


    }

    public void refreshHeader_titles(List<String> events) {
        this.header_titles.clear();
        this.header_titles.addAll(header_titles);
        notifyDataSetChanged();
    }

    ;


    @Override
    // From this Override  method How to Return how many elements are the Group count like parent
    public int getGroupCount() {
        return header_titles.size();
    }


    @Override
    //here  the number of child items are in each heading. There are three Heading - PAtien Name ,Age, Gender
    public int getChildrenCount(int groupPosition) {

//        Log.d("xxx", )
        return child_titles.get(header_titles.get(groupPosition)).size(); //how to Return the size of HashMap

    }


    @Override
    public Object getGroup(int groupPosition) {
        return header_titles.get(groupPosition);
    }

    @Override
    // here how to retuen child items on the particular headings and Positions.
    public Object getChild(int groupPosition, int childPosition) {
        return child_titles.get(header_titles.get(groupPosition)).get(childPosition);
    }

    @Override
    //return the groupo position
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    @Override
    // Here return the Group View
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        // Here how to get the Heading Title here Decalered String Variables, now how to get title of heading from getGroup methods so simple call Backed Methods.
        String title = (String) this.getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.prent, null);
        }
        TextView textView = (TextView) convertView.findViewById(R.id.heading_item);
        textView.setText(title);     //  for Heading bold style ,title
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        String title = (String) this.getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child, null);
        }

        String cellData = child_titles.get(header_titles.get(groupPosition)).get(childPosition);

        EditText childItem = (EditText) convertView.findViewById(R.id.child_item);
        childItem.setHint(cellData);

        childItem.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.d("xxxx", "Has focus " + hasFocus);
                if (hasFocus) {

                } else {

                }

                return;
            }
        });

        return convertView;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void setListData(HashMap<String, List<String>> lData) {
        child_titles = lData;
    }

    public void setHeaderData(List<String> hData) {
        header_titles = hData;
    }
}