繁体   English   中英

Android ListView为空

[英]Android ListView is empty

我有ListView。 我正在使用BaseAdapter来填充列表。 我列表不包含任何数据。 列表可见,但列表中没有数据。 这是我的

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;
}
}

这是我的活动类别,其中我的列表为空。

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是我从DbHelper类检索数据的方法。 在我的ModelString类中,没有什么比两个字符串更好。

logcat中没有错误。

ArrayList是简单的增长数组。 尝试添加元素时,如果超出缓冲区大小,则只会增加。 因此,初始大小可以是任何正值。

您在评论中说“我调试它,它显示size = 0和modCount = 0”,并且据此ArrayList实际上为空,因此没有数据供CustomAdapter显示在列表视图中。

至于“ addList.size();在Logcat中,它返回一个36”。 ArrayList是简单的增长数组。 当尝试添加元素时,缓冲区的大小已超出,只是在增加。 因此,初始大小可以是任何正值。 size()为1会太小。 即使有几个元素,我们也会有一些调整大小的操作.100会浪费空间。

因此,十是妥协。 为什么是10,而不是12或8? 第一个提示是,已经分析了典型的用例,这是性能损失与空间损失之间的最佳匹配。 但是,我认为,看到Sun的原始代码后,它并没有进行深入的分析,而是一个任意的“不要太小,不要太大”的数字。

在模型类中创建setter和getter。 那应该使您可以将项目放入列表视图。

arraylist应该包含模型类的对象。

暂无
暂无

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

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