I'm using a Custom list view to create a list view that contains a switch and a text view on each row. Each row is created from an xml file named row.xml and the list view is inside the main activity xml file. These 2 xml files can be seen below.
HomeActivity.xml
<TreeView>
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource Ancestortype={x:Type TreeView}},Path=DataContext.anyobjectofViewmodel}"/>
</Style>
</TreeView.Resources>
............
............
</TreeView>
row.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#0099cc" tools:context=".Activities.HomeActivity">
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#ff69539d">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="120dp"
android:layout_below="@+id/fullscreen_content_controls"/>
</RelativeLayout>
I use a custom list adapter to populate the listview with the text for the textfields. This can be seen below.
customlistAdapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Switch
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/switch"
android:checked="false"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Switch Name"
android:id="@+id/textView"
android:layout_alignBottom="@+id/switch"
android:layout_toRightOf="@+id/switch"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#000000" />
</RelativeLayout>
The list adapter is created from the main activity as you can see below.
MainActivity
public class customListAdapter extends BaseAdapter{
Context context;
String[] switchName;
private static LayoutInflater inflater = null;
public customListAdapter(Context context, String[] data) {
// TODO Auto-generated constructor stub
this.context = context;
this.switchName = data;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return switchName.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return switchName[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.row, null);
TextView text = (TextView) vi.findViewById(R.id.textView);
Switch mySwitch = (Switch) vi.findViewById(R.id.switch);
mySwitch.setTag(position);
text.setText(switchName[position]);
return vi;
}
}
The problem that I am having is being able to access the switches that are created within the custom list adapter from the main activity. I need to be able to maintain a reference to all of the switches from the main activity so that I can change the states. Ideally i'd be able to have a list or array of each of the switches in the mainActivity. Any help would be appreciated.
答案 0 :(得分:0)
in your adapter class, add something like:
_id=56bcab3a2df7994c5a073201&created_by=andrewwimley&case_desc=asdf&creation_timestamp=Thu+Feb+11+2016+09%3A39%3A38+GMT-0600+(CST)&date=Thu+Feb+11+2016+09%3A39%3A38+GMT-0600+(CST)&createdBy=andrewwimley&createDateTime=02%2F11%2F2016+09%3A39+am&endDateString=02-10-2016+11%3A59+pm&startDateString=02-10-2016+12%3A00+am&type=schedule_entry&__v=0&meeting_id=56bcab3a2df7994c5a073201&case_name=asdfFFFF&start_time=Thu+Feb+11+2016+09%3A39%3A38+GMT-0600+(Central+Standard+Time)&end_time=Thu+Feb+11+2016+09%3A39%3A38+GMT-0600+(Central+Standard+Time)&desc=asdf&mediator_LawyerIDs%5B%5D=475840&meeting_result=Manual
then in the adapter's private OnCheckChangedListener mOnCheckChangedListener;
public void setOnCheckChangedListener( OnCheckChangedListener listener ){
mOnCheckChangedListener = listener;
}
method, do something like:
getView
then in the activity, so what you want:
mySwitch.setOnCheckChangedListener( mOnCheckChangedListener );
and set it on the adapter:
private OnCheckChangedListener mOnCheckChangedListener = new OnCheckChangedListener(){
@Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked){
// keep a list of what was checked
}
}