简体   繁体   English

Android ListView为空

[英]Android ListView is empty

I have ListView. 我有ListView。 I am using BaseAdapter to inflate the List. 我正在使用BaseAdapter来填充列表。 I List does not contains any data. 我列表不包含任何数据。 List is visible but there is no data in list. 列表可见,但列表中没有数据。 Here my 这是我的

CustomAdapter.java 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 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 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. getAllData是我从DbHelper类检索数据的方法。 In my ModelString class there is nothing great than two strings. 在我的ModelString类中,没有什么比两个字符串更好。

There is no error in logcat. logcat中没有错误。

The ArrayList is simple growing array. ArrayList是简单的增长数组。 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. 您在评论中说“我调试它,它显示size = 0和modCount = 0”,并且据此ArrayList实际上为空,因此没有数据供CustomAdapter显示在列表视图中。

As for the "addList.size(); in Logcat it returns a 36" . 至于“ addList.size();在Logcat中,它返回一个36”。 The ArrayList is simple growing array. ArrayList是简单的增长数组。 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. size()为1会太小。 Even with a few elements we will have a few resize operations.The 100 would be a loss of space. 即使有几个元素,我们也会有一些调整大小的操作.100会浪费空间。

So, the 10 is compromise. 因此,十是妥协。 Why 10 and not 12 or 8? 为什么是10,而不是12或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. 但是,我认为,看到Sun的原始代码后,它并没有进行深入的分析,而是一个任意的“不要太小,不要太大”的数字。

Create setters and getters in the model class. 在模型类中创建setter和getter。 That should enable you get items into your listview. 那应该使您可以将项目放入列表视图。

The arraylist should contain objects of the model class. arraylist应该包含模型类的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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