我有ListView。我使用BaseAdapter來擴充列表。我列表不包含任何數據。列表可見,但列表中沒有數據。 這裏我Android ListView爲空
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中沒有錯誤。
你確定'addList'不是空的嗎?你有沒有嘗試交換最後兩行? –
你的代碼看起來很好。嘗試在Logcat的'addList'count中打印。 –
我調試它,它顯示大小= 0和modCount = 0 –