0
我正在使用自定义列表视图来创建包含每行上的开关和文本视图的列表视图。每行都是从一个名为row.xml的xml文件创建的,列表视图位于主活动xml文件内。这2个XML文件可以在下面看到。使用自定义列表视图时访问行项目android
HomeActivity.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>
row.xml
<?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>
我使用自定义列表适配器来填充与文本的文本框列表视图。这可以在下面看到。
customlistAdapter
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;
}
}
名单适配器与主要活动创建为你可以看到下面。
MainActivity
public class MainActivity extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(new customListAdapter(this, new String[]{"Switch 1", "Switch 2 ", "Switch 3", "Switch 4", "Switch 5", "Switch 6", "Switch 7"}));
}
}
,我有是能够访问由主要活动自定义列表适配器中创建的交换机的问题。我需要能够保留对主要活动中所有开关的引用,以便我可以更改这些状态。理想情况下,我可以在mainActivity中拥有每个开关的列表或数组。任何帮助,将不胜感激。
这将告诉我何时检查其中一个开关,但是是否可以通过编程方式将主开关的状态改变?所以像myswitch.setChecked(true);我觉得为了能够做到这一点,我需要以某种方式列出存储在主要活动中的所有交换机的列表。 – Kraze
@Kraze你可以在'onCheckChangedListener'中做任何你想做的事情,你可以在'buttonView'参数中访问switch实例 – momo
没有机会尝试这个,但我的理解是,当使用onCheckChangedListener时,我不会是否能够访问交换机,直到用户手动切换?如我错了请纠正我。 – Kraze