0

I have ListView with row as TextView and Switch.
Now I want that When I click on First Item Switch i.e. when I on the Switch(position==0) Then all Rows below that are Visible else When I make First Switch Off then all bellow Rows have to be removed all gone.
How can I achive this..?
By default switch is Off in android that mean first time only one row should be displayed and when I on that switch all remaining rows are added to below first row and vise versa.
Following Code I have Used :

public class MainActivity extends Activity   
 {
     ListView listView;
     private static final String[] items=
      { "Allow Billing Details Capture", "Address1", "Address2", "City", "State", "Zip",
          "Country", "Email","Phone" };
 static int i;
RelativeLayout Rlmain;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);  
    final ArrayList<String> data=new ArrayList<String>(Arrays.asList(items));
    final TestAdapter adapter=new TestAdapter(this,R.layout.fieldlist,data);
     listView=(ListView)findViewById(R.id.listView1);
    listView.setAdapter(adapter);

}
private void FillList()
{   ArrayList<String> currentdata=new ArrayList<String>();
    ArrayList<String> data=new ArrayList<String>(Arrays.asList(items));
    for(int i=1;i<data.size();i++)
    {
        currentdata.add(data.get(i));
    }
    TestAdapter adapter=new TestAdapter(this,R.layout.fieldlist,currentdata);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}
private void fillone() 
{
    ArrayList<String> currentdata=new ArrayList<String>();
    ArrayList<String> data=new ArrayList<String>(Arrays.asList(items));
    currentdata.add(data.get(0));
    TestAdapter adapter=new TestAdapter(this,R.layout.fieldlist,currentdata);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}
public class TestAdapter extends ArrayAdapter<String> {
    Context con;
    ArrayList<String> listdata;

    public TestAdapter(Context context, int textViewResourceId,ArrayList<String> myitem) 
    {
        super(context, textViewResourceId, myitem);
        this.con = context;
        this.listdata = myitem;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //final View v=convertView;     
        if (convertView == null) 
        {
            convertView = inflater.inflate(R.layout.fieldlist, null);
        }
        TextView data = (TextView) convertView.findViewById(R.id.VTItemTextView);
        Switch check=(Switch)convertView.findViewById(R.id.Vtcheck);
        if(position==0 && check.isChecked())
        {
            FillList();
            notifyDataSetChanged();
        }
        else
        {
            fillone();
            notifyDataSetChanged();
        }
        check.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {               
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {       
                if(isChecked && position==0 )
                {                       
                    FillList();
                    notifyDataSetChanged();                 
                }
                else 
                {
                    fillone();
                    notifyDataSetChanged();
                }

            }
        });
        data.setText(listdata.get(position));
        return convertView;
    }
}
  }


As it is firstly showing one row only but not changing when I on that switch.

Pravin
  • 1,362
  • 12
  • 21

1 Answers1

0

I think what you actually need is an Expandable listview like here or an Accordian like here

Basim Sherif
  • 5,384
  • 7
  • 48
  • 90