listview行高

时间:2013-02-19 19:43:20

标签: android listview

我试图在不制作自定义列表视图和新的arrayadapter的情况下设置listview行高。 我正在设置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;

1 个答案:

答案 0 :(得分:1)

  

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)

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

你的方法太乱了,不正确。我建议您为行创建XML布局,然后使用此布局对convertView进行充气,然后使用Cursor中的数据设置相应的小部件(textViews)。

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