0

我正在尝试设置 listview 行高而不制作自定义 listview 和新 arrayadapter。我将高度设置为 LayoutParams.WRAP_CONTENT;我在 logcat 中得到了这个

02-19 20:42:25.721: E/AndroidRuntime(6936): at android.widget.AbsListView.obtainView(AbsListView.java:2465) 02-19 20:42:25.721: 
                    E/AndroidRuntime(6936): at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)

这是我的查看代码:

public View getView(int position, View convertView, ViewGroup parent) {
                 System.gc();
                 TextView tv = new TextView(mContext.getApplicationContext());
                 LayoutInflater inflater = getLayoutInflater();
                 LayoutParams params = tv.getLayoutParams();
                 String id = null;
              mRowHeight = LayoutParams.WRAP_CONTENT;
                 if (params == null) {
                       music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                       musiccursor.moveToPosition(position);
                       id = musiccursor.getString(music_column_index);
                       music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
                       musiccursor.moveToPosition(position);
                       id += musiccursor.getString(music_column_index);
                       tv.setText(id);
                       params.height = mRowHeight;
                 } else
                       tv = (TextView) convertView;
                 return tv;
4

1 回答 1

1

E/AndroidRuntime(6936): 在 android.widget.AbsListView.obtainView(AbsListView.java:2465) 02-19 20:42:25.721: E/AndroidRuntime(6936): 在 android.widget.ListView.measureHeightOfChildren(ListView.java :1250)

您的错误意味着该getView()方法将null在某些情况下返回。我对您的代码有点困惑,convertView如果为空,您无处可膨胀。

您的方法太混乱且不正确。我建议您为行创建XML布局,然后使用此布局进行膨胀,convertView然后使用来自Cursor.

if (convertView == null) {
   convertView = inflater.inflate(R.layout.someRowLayout, null, false);
   // if you have RowHolder so set him as tag to convertView
}
else {
      // just recycle it if you have holder
}
if (cursor.moveToPosition(position)) {
   String data = cursor.getString(cursor.getColumnIndex(Constants.COLUMN_NAME));
   if (data != null && data.length() > 0) {
      TextView t = findViewById(R.id.someId);
      t.setText(data);
   }
}
于 2013-02-19T19:45:44.217 回答