Android自定义列表,意外关闭应用程序

时间:2013-03-23 08:29:10

标签: android android-layout listview android-ui custom-lists

我使用以下代码在我的自定义列表中显示数据。但它关闭,但不支持的操作。 java.lang.UnsupportedOperationException:AdapterView不支持addView(View,LayoutParams)

public class VerifiedProblemsActivity extends Activity {
    ArrayList<Problem> problemsList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.d("IN VERIFIED ACTIVITY", "ACTIVITY");
        setContentView(R.layout.verified_problems);

        ListView problemsListView = (ListView) findViewById(R.id.problemsList);

        ArrayList<Problem> problemsList = new ArrayList<Problem>();

        Problem p1 = new Problem();
        p1.problemName = "Problem 1";
        p1.problemComments = "Comments 1";

        Problem p2 = new Problem();
        p2.problemName = "Problem 1";
        p2.problemComments = "Comments 1";

        problemsList.add(p1);
        problemsList.add(p2);

        problemsListView.setAdapter(new ProblemAdapter(getApplicationContext(),
                android.R.layout.simple_list_item_1, problemsList));
    }

    private class Problem {
        public String problemName;
        public String problemComments;

        public Problem() {
            problemName = "";
            problemComments = "";
        }
    }

    private class ProblemAdapter extends ArrayAdapter<Problem> {
        ArrayList<Problem> problemObjects;

        @SuppressWarnings("unchecked")
        public ProblemAdapter(Context context, int textViewResourceId,
                List objects) {
            super(context, textViewResourceId, objects);
            problemObjects = new ArrayList<Problem>(objects);

            // TODO Auto-generated constructor stub
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.list_item, parent);
            try {


                TextView title = (TextView) row
                        .findViewById(R.id.tvProblemTitle);
                TextView description = (TextView) row
                        .findViewById(R.id.tvProblemDecription);

                title.setText(problemsList.get(position).problemName);
                description
                        .setText(problemsList.get(position).problemComments);


            } catch (Exception e) {
                Log.d("VERIFIED PROBLEM ACTIVITY", e.getMessage());
            }
            return row;
        }
    }
}

以下是我正在使用的列表项布局。

<?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"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:gravity="center_vertical" >

        <ImageView
            android:id="@+id/imgProblemImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="5dp"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvProblemTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tvProblemDecription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Small Text"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:3)

  

java.lang.UnsupportedOperationException:addView(View,LayoutParams)   AdapterView

不支持

我认为问题是:

View row = inflater.inflate(R.layout.list_item, parent);

AdapterView不允许添加新的View,因此您需要将null作为ViewGroup传递。

View row = inflater.inflate(R.layout.list_item, null, false);

现在它应该有效。让我知道。

答案 1 :(得分:0)

问题在视野中。 改变这些方法

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent);

喜欢这个

View row  = convertView
if(row==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, null);
}
希望这会对你有所帮助。