所以我有一个片段,一旦进度条加载完毕,就会出现一个项目的列表视图,但是当LogCat显示我确实收集了这些项目时,屏幕上没有任何内容。为什么是这样?这是我的代码和xml:
爪哇:
private class showView extends AsyncTask<Object,Void,Boolean>{
Ride[] rides;
ArrayList<String>aList = new ArrayList<String>();
List<Ride> ri = new ArrayList<Ride>();
String[] rideInfo;
String sessionToken;
String deviceId;
String startZip;
long start;
protected void onPreExecute(){
deviceId = device.getDeviceId();
sessionToken = SessionManager.getSessionToken(getActivity());
if(sessionToken == null){
MainActivity.startSignIn(ctx);
}
}
protected Boolean doInBackground(Object... stuff){
Log.d("addressList", addressList.toString());
startZip = addressList.get(0).getPostalCode();
start = (long)c.get(Calendar.DATE);
reRi = RestQuery.rideSearch(sessionToken, deviceId, startZip, 20, null, 0, start, start);
if(!(reRi == null)){
ri = reRi.getRides();
if(!(ri == null)){
rides = ri.toArray(new Ride[ri.size()]);
if(!(rides == null)){
for(int x = 0; x < rides.length; x++){
String name = rides[x].getName();
String type = rides[x].getType();
String date = rides[x].getStartDate();
String startL = rides[x].getStartLocation();
String endL = rides[x].getEndLocation();
String rideI = type + "/n" + name + "/n" + date + "/n" + startL + "-" + endL;
Log.d("ride info",rideI);
System.out.println(rideI);
aList.add(rideI);
}
rideInfo = aList.toArray(new String[aList.size()]);
}
else{
MainActivity.startSignIn(ctx);
}
}
else{
MainActivity.startSignIn(ctx);
}
}
else{
MainActivity.startSignIn(ctx);
}
return true;
}
public void onPostExecute(Boolean results){
results = false;
if(reRi.getResult().getType().equalsIgnoreCase("Ok") || reRi.getResult().getMessage().getCode().equalsIgnoreCase("I00001")){
results = true;
}
if(results){
Adapter adapter = new Adapter(ctx,rideInfo);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
switcher.showNext();
}
else if(!results){
if(reRi.getResult().getMessage().getCode().equalsIgnoreCase("E00012") || reRi.getResult().getMessage().getCode().equalsIgnoreCase("E00011")){
MainActivity.startSignIn(ctx);
}
else{
Toast.makeText(ctx, "Couldn't access rides", Toast.LENGTH_SHORT).show();
}
}
}
}
private class Adapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public Adapter(Context context, String[] values) {
super(context, R.layout.browse_fragment, values);
this.context = context;
this.values = values;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.browse_fragment, parent, false);
TextView txtView = (TextView)rowView.findViewById(R.id.yo);
txtView.setText(Html.fromHtml(values[position]));
return rowView;
}
}
}的xml:
<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/browse_fragment_switcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<TextView
android:id="@+id/sup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Finding possible rides"
android:textSize="25sp"/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/llCollection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "vertical">
<ListView
android:id="@+id/show_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@+id/yo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No rides could be found at this time"
android:textSize="25sp"/>
</LinearLayout>
</ViewSwitcher>
答案 0 :(得分:0)
我相信你需要打电话:
adapter.notiftyDataSetChanged();
更新aList内容后。
http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()
试试看,让我知道它是否有帮助。
如果你想发布你的其余代码(如果,你在哪里启动异步任务),我可以提供更多的评论。