0

我有一個列表視圖,應該從AsyncTask類的ParseObjects填充。從logcat中,asynctask成功從服務器檢索對象。但是,這些對象不會顯示在列表視圖中。我爲此使用了一個片段。請幫忙。 這是片段類。Android的ListView不顯示片段中的行

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

這是一種危險的空空你的任務後,初始化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(); 
} 

=>基本上,適配器在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; 
} 
+0

沒有工作 – user3679294 2014-10-09 11:25:42

+0

您可以加入你的XML佈局文件? – ToYonos 2014-10-09 11:36:22

+0

我已添加。 – user3679294 2014-10-09 12:07:48