2013-03-23 53 views
1

我正在使用以下代碼在我的自定義列表中顯示數據。但它關閉,不受支持的operaton例外。 java.lang.UnsupportedOperationException:addView(查看,的LayoutParams)沒有在適配器視圖Android自定義列表,意外關閉應用程序

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> 

回答

3

java.lang.UnsupportedOperationException:addView(查看,的LayoutParams) 沒有在適配器視圖

支持我覺得問題是這樣的:

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

適配器視圖不允許添加新的視圖,所以你需要傳遞null作爲ViewGroup。

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

現在它應該可以工作。讓我知道。

+0

仍然無法正常工作。 – mdanishs

+0

@Dani應該嘗試添加false作爲第三參數。 – Sajmon

+0

謝謝,它的工作原理... :) @Sajmon – mdanishs

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

希望這對您有所幫助。