我有ListView
row asTextView
和Switch
.
现在我想要它,当我单击第一项Switch
时,即当我在Switch
(位置==0)上时,下面的所有行都是可见的,否则当我第一次关闭时,所有波纹管行都必须全部删除。
我怎样才能做到这一点..?
默认情况下switch
,在 android 中为 Off,这意味着第一次应该只显示一行,当我打开时,switch
所有剩余的行都添加到第一行下方,反之亦然。
以下我使用过的代码:
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;
}
}
}
因为它首先只显示一行,但在我打开该开关时不会改变。