简体   繁体   中英

Android ListView is empty

I have ListView. I am using BaseAdapter to inflate the List. I List does not contains any data. List is visible but there is no data in list. Here my

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

ArrayList<ModelString> list;
ListView lv;
private Context context;
DbHelper dbHelper;
private LayoutInflater inflater = null;

public CustomAdapter(Context context, ArrayList<ModelString> result){

    this.list=result;
    this.context = context;
    dbHelper = new DbHelper(context);
    inflater = (LayoutInflater) (this.context)
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


@Override
public int getCount() {

    return list.size();
}

@Override
public Object getItem(int position) {

    return position;
}

@Override
public long getItemId(int position) {

    return position;
}

class Holder {

    public TextView textView1,textView2;

}

@Override
public View getView(int position, View rootView, ViewGroup parent) {

    final ModelString tempData = list.get(position);
    //Holder h = new Holder();
    Holder h = null;
    if (rootView == null){

        rootView = inflater.inflate(R.layout.row, null);
        h = new Holder();
        h.textView1 = (TextView) rootView.findViewById(R.id.mainTextView);
        h.textView2 = (TextView) rootView.findViewById(R.id.subTextView);
        rootView.setTag(h);

    } else {

        h = (Holder) rootView.getTag();

    }

    h.textView1.setText(tempData.busScheduleAt);
    h.textView2.setText(tempData.sourceDestination);

    return rootView;
}
}

and this is my activity class where my list is empty.

YouAreAt.java

public class YouAreAt extends Activity {

ListView stopsList;
EditText searchEditText;

ArrayList<ModelString> addList;
CustomAdapter myAdapter;

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

    final DbHelper dbHelper = new DbHelper(this);

    stopsList = (ListView)findViewById(R.id.stopsList);
    searchEditText = (EditText)findViewById(R.id.searchEditText);

    addList = new ArrayList<ModelString>();
    addList = dbHelper.getAllData("1");
    myAdapter = new CustomAdapter(this, addList);
    myAdapter.notifyDataSetChanged();
    stopsList.setAdapter(myAdapter);
}
}

activity_you_are_at.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ashu.busindicator.YouAreAt" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="#000000">

    <EditText
        android:id="@+id/searchEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search"
        android:textColor="#FFFFFF"
        android:ems="10" >

    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/stopsList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

</LinearLayout>

getAllData is my method to retrieve data from DbHelper class. In my ModelString class there is nothing great than two strings.

There is no error in logcat.

The ArrayList is simple growing array. When trying to add element, and the buffer size is exceeded, it is simply growing. So the initial size can be any positive value.

You said in your comments "I debug it, it shows size=0 and modCount=0" well as per this the ArrayList is actually empty and hence there is no data for your CustomAdapter to display in your listview.

As for the "addList.size(); in Logcat it returns a 36" . The ArrayList is simple growing array. When trying to add element, the buffer size is exceeded, it is simply growing. So the initial size can be any positive value. The size() being 1 would be too little. Even with a few elements we will have a few resize operations.The 100 would be a loss of space.

So, the 10 is compromise. Why 10 and not 12 or 8? First hint, that the typical use cases were analysed and this is the best fit between lost of performance and lost of space. However, I think, seeing the Sun's original code, that it wasn't analysed so deeply and it is an arbitrary 'not too small, not too big' number.

Create setters and getters in the model class. That should enable you get items into your listview.

The arraylist should contain objects of the model class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM