我有一个listview,应该使用AsyncTask类中的ParseObjects填充。从logcat开始,asynctask成功从服务器中检索对象。但是,这些对象不会显示在列表视图中。我正在使用一个片段。请帮忙。 这是片段类。
public class HomeFragment extends Fragment{
public HomeFragment(){}
String theLocationOfCinema;
List<ParseObject> result;
View rootView;
ProgressDialog mProgressDialog;
ListView listView;
GridViewAdapter mCardArrayAdapter;
private List<Movie> cards = null;
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
new RemoteDataTask().execute();
listView = (ListView) getActivity().findViewById(R.id.sampleListView);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
theLocationOfCinema = this.getArguments().getString("location");
rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
private class RemoteDataTask extends AsyncTask<Void, Void, List<Movie>>{
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
//mProgressDialog.setTitle("Parse.com Custom ListView Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected List<Movie> doInBackground(Void... params) {
cards = new ArrayList<Movie>();
HashMap<String, Object> paramss = new HashMap<String, Object>();
paramss.put("theLocation", theLocationOfCinema);
ParseCloud.callFunctionInBackground("getMovieIdsInCinemass", paramss, new FunctionCallback<ArrayList<ParseObject>>() {
public void done(ArrayList<ParseObject> ratings, ParseException e) {
if (e == null) {
result = new ArrayList<ParseObject>();
Set<String> titles = new HashSet<String>();
for(ParseObject item : ratings) {
if(titles.add(item.getObjectId())) {
result.add(item);
}
}
for(ParseObject human : result )
{
Movie map = new Movie();
map.setTitle((String) human.get("Title"));
cards.add(map);
//Log.e("cloud code example", "response: " + human.getObjectId());
Log.e("cloud code example", "response: " + human.get("Title"));
}
//Log.e("cloud code example", "response: " + ratings);
}
}
});
return cards;
//return null;
}
@Override
protected void onPostExecute(List<Movie> result) {
super.onPostExecute(result);
// Pass the results into ListViewAdapter.java
mCardArrayAdapter = new GridViewAdapter(getActivity(), result);
// Binds the Adapter to the ListView
if (listView != null) {
listView.setAdapter(mCardArrayAdapter);
}
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
这是适配器类
public class GridViewAdapter extends ArrayAdapter<Movie>{
private Context context;
private List<Movie> movieslist;
public GridViewAdapter(Context context, List<Movie> movieslist) {
super(context, R.layout.movies_row_item, movieslist);
this.context = context;
this.movieslist = movieslist;
}
private static class ViewHolder {
TextView title;
}
@Override
public int getCount() {
return movieslist.size();
}
@Override
public Movie getItem(int position) {
return movieslist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public List<Movie> getList(){
return movieslist;
}
public void setItemList(List<Movie> ilist){
this.movieslist = ilist;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.movies_row_item, parent, false);
Movie mv = movieslist.get(position);
TextView tt = (TextView) convertView.findViewById(R.id.text);
tt.setText(mv.getTitle());
return convertView;
}
}
这是xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/sampleListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="#CCCCCC"
android:dividerHeight="1dp" >
</ListView>
</FrameLayout>
答案 0 :(得分:0)
在执行任务后初始化listView
会有点危险,因为您不知道何时会发生OnPostExecute。试试这个:
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
listView = (ListView) getActivity().findViewById(R.id.sampleListView);
mCardArrayAdapter = new GridViewAdapter(getActivity());
listView.setAdapter(mCardArrayAdapter);
new RemoteDataTask().execute();
}
=&GT;基本上,onActivityCreated方法中的适配器初始化。
在OnPostExecute方法中,只需将结果放在适配器中:
protected void onPostExecute(List<Movie> result) {
super.onPostExecute(result);
// Pass the results into ListViewAdapter
mCardArrayAdapter.addAll(result);
mCardArrayAdapter.notifyDataSetChanged();
// Close the progressdialog
mProgressDialog.dismiss();
}
不要忘记将此构造函数添加到GridViewAdapter
public GridViewAdapter(Context context) {
super(context, R.layout.movies_row_item);
this.context = context;
}