質問

I am using the following code to to display data in my custom list. but it closes with an exception of unsupported operaton. java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

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;
        }
    }
}

and the following is the list item layout I am using.

<?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>
役に立ちましたか?

解決

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

I think problem is this:

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

AdapterView doesn't allow adding new View so you need to pass null as ViewGroup.

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

Now it should works. Let me know.

他のヒント

problem in view. change these methods

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

to like this

View row  = convertView
if(row==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, null);
}

hope this will be helpful to you.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top