arrayAdapter只返回一个位置,Android

Kev*_*vik 18 android android-arrayadapter

我将arrayList设置为12个项目,但是arrayAdapter只返回屏幕上的一个项目,为什么会这样?它应该在列表中显示12个项目.

  public class MainActivity extends Activity {

ArrayList<CheckBoxInfo> cfo = new ArrayList<CheckBoxInfo>();
CheckBoxInfo cbr;
private ListAdapter MyAdapter;
ListView listview;
MyAdapter myAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cbr = new CheckBoxInfo();
    cbr.checkBoxName = "dfdjklfjdkljf";
    cbr.checkBoxState = true;

      for(int i = 0; i <12; i++){
            cfo.add(cbr);
      }

      Toast.makeText(MainActivity.this, "size: " + cfo.size(), Toast.LENGTH_SHORT).show();

    listview = (ListView) findViewById(R.id.listView);
    myAdapter = new MyAdapter(cfo, this);
    listview.setAdapter(myAdapter);
}

public class MyAdapter extends ArrayAdapter<CheckBoxInfo> {

    private List<CheckBoxInfo> checkBoxList;
    private Context context;

    public MyAdapter(List<CheckBoxInfo> infoList, Context context) {
        super(context, R.layout.row_layout, infoList);
        this.checkBoxList = infoList;
        this.context = context;

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        // First let's verify the convertView is not null
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_layout, parent, false);
        }
            // Now we can fill the layout with the right values
            TextView tv = (TextView) convertView.findViewById(R.id.textView1);
            CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
            CheckBoxInfo cbi = checkBoxList.get(position);

              Toast.makeText(MainActivity.this, "position: " + position, Toast.LENGTH_SHORT).show();


            tv.setText(cbi.checkBoxName);

        return convertView;
    }

}  // end MyAdapter
  }
Run Code Online (Sandbox Code Playgroud)

row_layout.xml

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/LinearLayout1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" " />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

 </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Kev*_*vik 65

发现问题是ListView是在xml布局中的ScrollView内部,一旦我删除了外部ScrollView,ListView的所有项目都出现了.

出于某种原因,如果你封装嵌套在几个图层中的ListView,如果ScrollView中的ListView,它将只显示一个位置项

  • 非常,非常,真实.可悲的是没有证件.谢谢. (5认同)
  • 当前的文档(http://developer.android.com/reference/android/widget/ScrollView.html)说:"你永远不应该使用带有ListView的ScrollView,因为ListView负责自己的垂直滚动.最重要的是,这样做会击败ListView中用于处理大型列表的所有重要优化,因为它有效地强制ListView显示其整个项目列表以填充ScrollView提供的无限容器." (4认同)
  • 多么愚蠢,愚蠢的框架.很遗憾.时间流逝:3个小时. (4认同)
  • 你需要在`ScrollView`上设置`android:fillViewport ="true"`,它有`ListView`. (2认同)