本文介绍了arrayAdapter只返回一个位置,机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了​​数组列表为12个项目,但一个ArrayAdapter只返回屏幕上的一个项目,这是为什么?它应该显示12个项目的名单上。

 公共类MainActivity延伸活动{

ArrayList的< CheckBoxInfo> CFO =新的ArrayList< CheckBoxInfo>();
CheckBoxInfo社区康复;
私人ListAdapter MyAdapter;
ListView控件列表视图;
MyAdapter myAdapter;


@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    CBR =新CheckBoxInfo();
    cbr.checkBoxName =dfdjklfjdkljf;
    cbr.checkBoxState =真;

      的for(int i = 0; I< 12;我++){
            cfo.add(CBR);
      }

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

    列表视图=(ListView控件)findViewById(R.id.listView);
    myAdapter =新MyAdapter(CFO,这一点);
    listview.setAdapter(myAdapter);
}

公共类MyAdapter扩展ArrayAdapter< CheckBoxInfo> {

    私人列表< CheckBoxInfo>的CheckBoxList;
    私人上下文的背景下;

    公共MyAdapter(名单< CheckBoxInfo> infoList,上下文语境){
        超(背景下,R.layout.row_layout,infoList);
        this.checkBoxList = infoList;
        this.context =背景;

    }

    公共查看getView(INT位置,查看convertView,ViewGroup中父){

        //首先我们核实convertView不为空
        如果(convertView == NULL){
            //这是一个新的观点,我们膨胀的新布局
            LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_layout,父母,假);
        }
            //现在我们可以填写了正确的价值观布局
            TextView的电视=(TextView中)convertView.findViewById(R.id.textView1);
            复选框CB =(复选框)convertView.findViewById(R.id.checkBox1);
            CheckBoxInfo CBI = checkBoxList.get(位置);

              Toast.makeText(MainActivity.this,位置:+位置,Toast.LENGTH_SHORT).show();


            tv.setText(cbi.checkBoxName);

        返回convertView;
    }

} //结束MyAdapter
  }
 

row_layout.xml

 < XML版本=1.0编码=UTF-8&GT?;
  < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:ID =@ + ID / LinearLayout1
     机器人:layout_width =match_parent
     机器人:layout_height =match_parent
     机器人:方向=横向>

<复选框
    机器人:ID =@ + ID / checkBox1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=/>

<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=大文本
    机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?

 < / LinearLayout中>
 

解决方案

发现该问题是在ListView里面装的是一个滚动型的XML布局,一旦我删除了外部滚动型ListView中的所有项目出现了。

由于某些原因,如果您封装嵌套在几层一个ListView它会显示只有一个位置的项目,如果内部的滚动型ListView控件在

I set the arrayList to 12 items, however the arrayAdapter only returns one item on the screen, why is this? It is supposed to show 12 items on the list.

  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
  }

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>
解决方案

found out that the problem was the ListView was inside a ScrollView in the xml Layout, once i removed the outer ScrollView all the items of the ListView showed up.

for some reason if you encapsulate a ListView nested in several layers it will show only one position item if the ListView in inside a ScrollView

这篇关于arrayAdapter只返回一个位置,机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 18:16