我目前正在尝试从SQL查询中填充片段内的ListView。 ListView有一个名为list_order_layout_order_progress的自定义列表视图。一旦我到达特定片段,它就会崩溃。
任何帮助将不胜感激。提前谢谢!
On create View我在哪里声明了listview
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabe_fragment_order_progress, container, false);
OrderNumber = getArguments().getInt("OrderNoFinal");
ProductID = getArguments().getInt("ProductID");
ProductionID = getArguments().getInt("ProductionID");
lblOrderNo = (TextView) rootView.findViewById(R.id.txtProgOrderNo);
lblProduct = (TextView) rootView.findViewById(R.id.txtProgProduct);
listView = (ListView) rootView.findViewById(R.id.listView);
connectionClass = new ConnectionClass();
GetProductName();
PopulateOrderProgressList();
return rootView;
}
我设置数组适配器的代码
public void PopulateOrderProgressList(){
Connection con = connectionClass.CONN();
try {
String sql = "exec sp_Get_Production_Tasks\n@Production_ID = " + ProductionID + ",@Product_ID = " + ProductID;
final ArrayList list = new ArrayList();
PreparedStatement statement = con.prepareStatement(sql);
ResultSet rs = statement.executeQuery();
while (rs.next()) {
list.add(rs.getString("Task_Name"));
}
listView.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),R.layout.list_layout_orderprogress , list));
}
catch(SQLException e){
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
包含作为片段的ListView的布局:
<LinearLayout 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:orientation="vertical"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context="layout.OrderProgress">
<TextView
android:text="Order:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtProgOrderNo"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product:"
android:textStyle="bold"
android:layout_marginTop="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/ltxtProduct" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:layout_marginTop="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/txtProgProduct" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Start"
android:textColor="#FF4081"
android:layout_marginRight="200dp"
android:id="@+id/textView31" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF4081"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Finish"
android:id="@+id/textView32" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="8dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:id="@+id/listView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:id="@+id/button3"
android:layout_gravity="center_horizontal" />
</LinearLayout>
ListView的自定义布局视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="32dp"
android:id="@+id/checkBox5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView30"
android:gravity="center_vertical" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:id="@+id/checkBox6"
android:layout_gravity="right" />
崩溃时显示的错误消息:
08-25 21:38:12.731 25022-25022/dura_art.bigbyte E/AndroidRuntime: FATAL EXCEPTION: main
Process: dura_art.bigbyte, PID: 25022
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:393)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at android.widget.AbsListView.obtainView(AbsListView.java:2929)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
at android.widget.ListView.onMeasure(ListView.java:1212)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1560)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1192)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:733)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
at android.view.View.measure(View.java:20151)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.Activi
答案 0 :(得分:0)
ArrayAdapter只知道如何处理TextViews列表。您想要使用自定义视图列表,因此您必须创建自定义ArrayAdapter。这可以通过创建继承ArrayAdapter的子类来完成。
来自ArrayAdapter的Android documentation: “默认情况下,此类要求提供的资源ID引用单个TextView。”
以下是我第一次使用自定义ArrayAdapter时的教程:http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter