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