2 alllllllll alllllllll 于 2013.04.25 17:00 提问

自定义的lisView没有正确显示

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

实现代码:

protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
requestWindowFeature(1);
setContentView(R.layout.activity_main);
_listView = (ListView)findViewById(R.id.listView1);
final String[] arrayOfString = SMSHelper.getInstance(this).getSMSCategoryNames();
SMSHelper.getInstance(this).getSMSCountForCategory(arrayOfString[0]);
SMSHelper.getInstance(this).getSMSForCategory(arrayOfString[0]).get(0);
SMSHelper.getInstance(this).getSMSPreviewForCategory(arrayOfString[0], "eat");
MyCustomAdapter localMyCustomAdapter = new MyCustomAdapter(this, 0, getCategoriesData());
this.b = new Bundle();
_listView.setAdapter(localMyCustomAdapter);
_listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
  public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
  {
    Intent localIntent = new Intent(MainActivity.this, SmsList.class);
    localIntent.putExtra("Category", arrayOfString[paramAnonymousInt].toString());
    MainActivity.this.startActivity(localIntent);
  }
   });
  }
   class MyCustomAdapter extends ArrayAdapter<String>
{
Context ctx;
String[] listDataForView;
int paramArrayOfString;

public MyCustomAdapter(Context paramInt, int paramArrayOfString, String[] arg4)
{
  super(paramInt, paramArrayOfString, arg4);
  this.ctx = paramInt;
  this.paramArrayOfString = paramArrayOfString;
  this.listDataForView=arg4;
}

private String getCategoriesLength(int paramInt)
{
  return SMSHelper.getInstance(this.ctx).getSMSCountForCategory(this.listDataForView[paramInt].toString());
}
public class MyCustomHolder {
    public TextView  localTextView1,localTextView2;;

}
@Override
public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup)
{
     View localView=paramView;
     MyCustomHolder holder = null;


  if(localView==null){
      localView = MainActivity.this.getLayoutInflater().inflate(R.layout.listitem, paramViewGroup);
      holder = new MyCustomHolder();
      holder.localTextView1 = (TextView)localView.findViewById(R.id.textView1);
      holder.localTextView2 = (TextView)localView.findViewById(R.id.textView2);

      localView.setTag(holder);
  }
  else
  {
      holder = (MyCustomHolder)localView.getTag();
  }
  if (paramInt % 2 == 0){
      localView.setBackgroundColor(Color.parseColor("#FFDDFEFE"));
    }

  holder.localTextView1.setText(listDataForView[paramInt].toString());
  holder.localTextView2.setText(getCategoriesLength(paramInt)); 

  return localView;

}
}

1个回答

jnhoodlum
jnhoodlum   2013.04.26 15:00
已采纳

应该是你的布局文件错了,看一下你的ListView是不是有子View,即
< ListView >
< XxxView >
< / XxxView >
< / ListView >
你把< XxxView >去掉就行了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!