我目前正在尝试将一个功能编码到一个应用程序中,允许它从列表中随机选择一些人。我使用List Fragment编写了一个列表,该列表根据可以输入池以供随机选择的合格成员的数量动态扩展和收缩。附件是活动的预览:enter image description here
我遇到了问题,因为我不确定如何检索构成列表的EditTexts的内容。我花了好几个小时试图在互联网的帮助下解决这个问题,但一直无法找到可靠的解决方案或指南。任何信息都有帮助。相关源代码如下:
public class PoolListFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<MemberSelector.members;i++){
HashMap<String, String> hm = new HashMap<String,String>();
//if stmts format spacing
if(i<9)
hm.put("num", "Member #"+(i+1)+": ");
if(i>8)
hm.put("num", "Member #"+(i+1)+": ");
hm.put("name", MemberSelector.names.get(i));
aList.add(hm);
}
// Keys used in Hashmap
String[] from = {"num","name"};
int[] to = {R.id.memberNumTV, R.id.memberNameET};
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.fragment_pool, from, to);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
片段布局组件:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragmentPoolLL">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.kensingtech.benjamin.sodzofieldtoolbox.PoolListFragment"
android:id="@+id/fragmentPool" />
</LinearLayout>
布局在列表片段中形成可视元素的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="wrap_content"
android:background="@color/colorAccent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/memberNumTV"
android:textColor="#000000" />
<EditText
android:layout_width="200dp"
android:layout_height="25dp"
android:inputType="textCapWords"
android:text=""
android:ems="10"
android:id="@+id/memberNameET"
android:background="#ffffff"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"/>
</LinearLayout>
</RelativeLayout>