代码之家  ›  专栏  ›  技术社区  ›  Kevik

arrayAdapter只返回一个位置,Android

  •  18
  • Kevik  · 技术社区  · 13 年前

    我将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
      }
    

    行布局.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>
    
    5 回复  |  直到 13 年前
        1
  •  67
  •   Kevik    13 年前

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

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

        2
  •  2
  •   Nermeen    13 年前

    以(权力)否决 getCount 在您的 MyAdapter

    public int getCount() {
       return infoList == null ? 0 : infoList.size();
    }
    
        3
  •  1
  •   Tuan Vu    13 年前

    请显示的代码 row_layout ,我认为你的错误是 for 循环:

    尝试创建2个复选框并添加手动测试:

    cbr1 = new CheckBoxInfo();
    cbr1.checkBoxName = "checkbox1";
    cbr1.checkBoxState = true;
    cfo.add(cbr1);
    
    cbr2 = new CheckBoxInfo();
    cbr2.checkBoxName = "checkbox2";
    cbr2.checkBoxState = true;
    cfo.add(cbr2);
    
        4
  •  1
  •   Sᴀᴍ Onᴇᴌᴀ    8 年前

    我遇到这个问题是因为 notifyDataSetChanged() 处于循环中,并且在某些情况下(如果没有数据项)没有被调用。

    确保 通知数据集已更改() 该叫的时候却叫了!

        5
  •  1
  •   SRKS    8 年前

    android:fillViewport=“true”上的ScrollView将解决这个问题。