【问题标题】:Android ListView not showing rows in fragmentAndroid ListView 不显示片段中的行
【发布时间】:2014-10-09 10:51:11
【问题描述】:

我有一个列表视图,它应该使用 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>

【问题讨论】:

    标签: android android-listview android-asynctask parse-platform android-arrayadapter


    【解决方案1】:

    在启动您的任务后初始化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;
    }
    

    【讨论】:

    • 你能添加你的xml布局文件吗?
    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多