arrayAdapter only returning one position, Android

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

  • getLocationOnScreen() returns zero
  • how to fix a bottom relative layout when soft keyboard comes?
  • Cordova build error after update to 3.5.0
  • Gradle Robolectric Resources NotFoundException in Testing
  • How do I create the semi-transparent grey tutorial overlay in Android?
  • Embed a custom View inside a WebView
  •   <?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>
    

    Related posts:

    How get coordinate of a ClickableSpan inside a TextView?
    Android Gauge Animation Question
    Using ASYNC Task to load map pins
    Adding an assets folder in Android Studio
    Is there some limits in android&#039; bundle?
    Implementing extended/custom View throws NoSuchMethod for constructor
  • How “deliveryIntent” works in Android SMS framework?
  • How to add a page to the beginning of ViewPager?
  • How to make an Android SlidingDrawer slide out from the left?
  • Get GPS Location in a Broadcast Receiver/or Service to Broadcast Receiver data transfer
  • dispatchKeyEvent calling method twice
  • Transitive dependencies for local aar library
  • 4 Solutions collect form web for “arrayAdapter only returning one position, Android”

    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

    Override getCount in your MyAdapter

    public int getCount() {
       return infoList == null ? 0 : infoList.size();
    }
    

    Please show code of row_layout, I think your error is for loop:

    Try create 2 checkboxs and add manual to test:

    cbr1 = new CheckBoxInfo();
    cbr1.checkBoxName = "checkbox1";
    cbr1.checkBoxState = true;
    cfo.add(cbr1);
    
    cbr2 = new CheckBoxInfo();
    cbr2.checkBoxName = "checkbox2";
    cbr2.checkBoxState = true;
    cfo.add(cbr2);
    

    I had this problem because my notifyDataSetChanged() was inside a loop and wasn’t being called in certain circumstances (if there were no data items).

    Make sure notifyDataSetChanged() is getting called when it should be!

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.