Android 数组适配器 - 在每一行中插入一个复选框
Android Array Adaptor - insert a checkbox into each row
是的,我有一个导航栏布局。当我的导航栏中的 stand1 按钮被按下时,content_main 中的 <FrameLayout>
被替换为 stand1.xml。这没有任何问题。
Content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></FrameLayout>
</RelativeLayout>
stand1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Stand1list">
</ListView>
</LinearLayout>
来自主要 activity
if (id == R.id.Stand1) {
fm.beginTransaction().replace(R.id.content_frame, new Stand1()).commit();
setTitle(R.string.Stand1);
按照在线教程,这是我的 Stand1.java 文件,用于用字符串数组填充列表
public class Stand1 extends Fragment {
ListView mList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.stand1,container,false);
mList = (ListView) root.findViewById(R.id.Stand1list);
return root;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
populateListView();
}
private void populateListView() {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.row_layout,pair);
mList.setAdapter(adapter);
}
}
和row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android" />
这是有效的,它显示了 5 个字符串。但是我想做到这一点,所以我也可以在每一行中有一个复选框。
如果我尝试将 linear layout
放在 row_layout.xml 中,我会收到错误消息
ArrayAdapter requires the resource ID to be a TextView
我正在关注 Whosebug,它对其他错误进行了排序,这表明我必须在 class 级别创建列表并在 onCreate 中将其膨胀。
我知道我可能必须制作自定义数组适配器,但我的问题是如何处理创建时视图的 inflation。
我不太确定我该怎么做。那么有人可以从这里告诉我,我如何在每一行中都有一个复选框。
感谢帮助
ArrayAdapter
需要知道将字符串数组中的文本放在哪里。默认情况下,它假定您传递给它的布局是 TextView
并将其文本设置为字符串之一。如果你传递给它一个更复杂的布局,你需要告诉它你想要它使用的 TextView
的 id。您可以使用 this constructor.
是的,我有一个导航栏布局。当我的导航栏中的 stand1 按钮被按下时,content_main 中的 <FrameLayout>
被替换为 stand1.xml。这没有任何问题。
Content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></FrameLayout>
</RelativeLayout>
stand1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Stand1list">
</ListView>
</LinearLayout>
来自主要 activity
if (id == R.id.Stand1) {
fm.beginTransaction().replace(R.id.content_frame, new Stand1()).commit();
setTitle(R.string.Stand1);
按照在线教程,这是我的 Stand1.java 文件,用于用字符串数组填充列表
public class Stand1 extends Fragment {
ListView mList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.stand1,container,false);
mList = (ListView) root.findViewById(R.id.Stand1list);
return root;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
populateListView();
}
private void populateListView() {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.row_layout,pair);
mList.setAdapter(adapter);
}
}
和row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android" />
这是有效的,它显示了 5 个字符串。但是我想做到这一点,所以我也可以在每一行中有一个复选框。
如果我尝试将 linear layout
放在 row_layout.xml 中,我会收到错误消息
ArrayAdapter requires the resource ID to be a TextView
我正在关注 Whosebug,它对其他错误进行了排序,这表明我必须在 class 级别创建列表并在 onCreate 中将其膨胀。
我知道我可能必须制作自定义数组适配器,但我的问题是如何处理创建时视图的 inflation。
我不太确定我该怎么做。那么有人可以从这里告诉我,我如何在每一行中都有一个复选框。
感谢帮助
ArrayAdapter
需要知道将字符串数组中的文本放在哪里。默认情况下,它假定您传递给它的布局是 TextView
并将其文本设置为字符串之一。如果你传递给它一个更复杂的布局,你需要告诉它你想要它使用的 TextView
的 id。您可以使用 this constructor.