我有ListView。我使用BaseAdapter来膨胀列表。I List不包含任何数据。列表是可见的,但列表中没有数据。这里我的
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是一个简单的增长型数组。当尝试添加元素,并且超出缓冲区大小时,它只是在增长。因此,初始大小可以是任何正值。
你说在你的评论"我调试它,它显示的大小=0和modCount=0"以及据此ArrayList实际上是空的,因此没有数据为您的CustomAdapter显示在您的列表视图。
对于"addList.size();在Logcat中,它返回一个36"。ArrayList是一个简单的增长型数组。当尝试添加元素时,超出了缓冲区大小,它只是在增长。所以初始大小可以是任意正值。尺寸()如果是1就太小了。即使只有几个元素,我们也会有一些调整大小的操作。100会浪费空间。
所以10是妥协。为什么是10个而不是12个或8个?第一个提示是,分析了典型的用例,这是性能损失和空间损失之间的最佳匹配。然而,我认为,看到太阳的原始代码,它没有被深入分析,它是一个任意的"不太小,也不太大"的数字。
在模型类中创建setter和getter。这应该能让你把项目放到listview中。
数组列表应该包含model类的对象。