ListView不显示片段

时间:2015-11-11 10:48:12

标签: android listview android-fragments android-listview

我正在使用listview来填充来自服务器的数据。此listview将在fragmentTab中显示该数据。现在数据解析正常,logcat也显示数据。但是listview没有填充数据。我试图通过调试看到错误,但没有问题。即使适配器中的 Holder 也会捕获数据,但不会显示。我不知道问题是什么。我尝试了以下问题,但没有得到答案。 Android - ListView in Fragment does not showing up

Android - ListView in Fragment does not show

ListView in Fragment Not Displayed

下面是我的片段和适配器。

Tastemaker Fragment:

public class TasteMakersFragment extends Fragment
{
     CommonClass commonTasteMakers;
String tasteMaker_url = CommonClass.url+ URLConstants.Host.URL_ALL_SUGGESTED_TASTEMAKERS;
String user_token = "8aa0dcd5aaf54c8a5aaef1aa242f342f";
ListView suggested_list;
List<SuggestedUserModel> suggestedUsers_list = new ArrayList<>();
SuggestedUsersAdapter mAdapter;
int selectedPosition = 0;

public TasteMakersFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
     View v = inflater.from(getActivity()).inflate(R.layout.suggested_users_tastemakers, null);

    commonTasteMakers = new CommonClass(getActivity().getApplicationContext());
    suggested_list = (ListView)v.findViewById(R.id.lst_suggestedUsers);

    new GetSuggestedUsers().execute(tasteMaker_url);

    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
}

private class GetSuggestedUsers extends AsyncTask< String, Void, Void>
{
    private ProgressDialog Dialog = new ProgressDialog(getActivity());

    protected void onPreExecute() {
        if (suggestedUsers_list.isEmpty())
        {
            Dialog = ProgressDialog.show(getActivity(),"Please be patient!","Fetching for first time...");
        }
    }

    @Override
    protected Void doInBackground(String... params)
    {
        if (suggestedUsers_list.isEmpty())
        {
            getSuggestedUsers();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
        else{
            Log.d("---- Already Fetched --", "---- Already Fetched ----");
            return null;
        }
    }

    protected void onPostExecute(Void unused)
    {
        Dialog.dismiss();
        mAdapter = new SuggestedUsersAdapter(getActivity(), suggestedUsers_list);
        suggested_list.setAdapter(mAdapter);
        suggested_list.setSelection(selectedPosition);
        mAdapter.notifyDataSetChanged();
        //startMainActivity();
    }
}

private List<SuggestedUserModel> getSuggestedUsers()
{
    StringRequest postRequest = new StringRequest(Request.Method.POST, tasteMaker_url, new Response.Listener<String>()
    {
        @Override
        public void onResponse(String response)
        {
            try {
                //JSONObject jsonResponse = new JSONObject(response).getJSONObject("form");
                JSONObject jsonResponse = new JSONObject(response);
                if (jsonResponse.has("data"))
                {
                    JSONObject data = jsonResponse.getJSONObject("data");
                    String code = jsonResponse.getString("code");

                    if(code.equals("200"))
                    {
                        if(data.has("tasteMakers"))
                        {
                            JSONArray tastemakers = data.getJSONArray("tasteMakers");
                            for (int i =0; i<tastemakers.length(); i++)
                            {
                                                                    JSONObject jsnObj = tastemakers.getJSONObject(i);
                                String id = jsnObj.getString("userId");
                                String name = jsnObj.getString("name");
                                String profilePic = jsnObj.getString("imgUrl");
                                Boolean isFollowed = jsnObj.getBoolean("isFollowed");

                                suggestedUsers_list.add(new SuggestedUserModel(id,
                                        name,
                                        profilePic,
                                        isFollowed));
                                Log.d("Names are ----", "size is=" + name +" and their id are: "+id);
                            }
                           // Log.d("Adapter list ----", "size is=" + suggestedUsers_list.size());
                        }
                    }
                    else {
                        Log.d("Error0 Error Error", "response------:" + jsonResponse);
                    }
                }
                // Log.d("response222---------","response22222------:"+jsonResponse);
            } catch (JSONException e) {
                Log.d("-----Stop------", "!!!");
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String> params = new HashMap<>();
            // the POST parameters:
            params.put("sessionToken", user_token);

            return params;
        }     };

    postRequest.setRetryPolicy(new DefaultRetryPolicy(20000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    Volley.newRequestQueue(getActivity().getApplicationContext()).add(postRequest);
    return suggestedUsers_list;
}

}

适配器类:

public class SuggestedUsersAdapter extends BaseAdapter {

Context context;
int count = 1;
private List<SuggestedUserModel> allUsers;

public SuggestedUsersAdapter(FragmentActivity activity, List<SuggestedUserModel> suggestUserModel)
{
    Log.d("Custom Adapter called", "" + suggestUserModel.size());
    context = activity;
    allUsers = suggestUserModel;
    //FragmentActivity mActivity = activity;
    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


@Override
public int getCount() {

    return allUsers.size();
    //    return imageId.length;
}

@Override
public Object getItem(int position) {
    return allUsers.get(position);
    //  return position;
}

@Override
public long getItemId(int position) {

    return position;
}

@SuppressLint({"ViewHolder", "InflateParams", "CutPasteId"})
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final mHolder holder;
    // int type = getItemViewType(position);
    LayoutInflater layoutInflater;
    getItemViewType(position);
    if (convertView == null)
    {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.suggested_users_tastemaker_cell, null);
        holder = new mHolder();

        //  convertView.setTag(mFeedsListItemViewHolder);
        holder.txt_suggestUserName = (TextView) convertView.findViewById(R.id.tv_tasteMakerName);
        holder.imgV_userProfile = (ImageView) convertView.findViewById(R.id.imgBtn_tasteMaker_pic);
        holder.btnFollow = (Button) convertView.findViewById(R.id.btnFollow_tasteMaker);
        holder.btnFollowing = (Button) convertView.findViewById(R.id.btnFollowing);

        convertView.setTag(holder);
    } else {
        holder = (mHolder) convertView.getTag();
    }

    final SuggestedUserModel suggestUser = allUsers.get(position);


    holder.txt_suggestUserName.setText(allUsers.get(position).getSuggested_UserName());

    /*if(suggestUser.getSuggested_UserImage().equals(""))
    {
        Picasso
                .with(context)
                .load(R.mipmap.ic_launcher)
                .transform(new CropSquareTransformationHomePage())
                .into(holder.imgV_userProfile);
    }
    else {
        Picasso
                .with(context)
                .load(suggestUser.getSuggested_UserImage())
                .transform(new CropSquareTransformationHomePage())
                .into(holder.imgV_userProfile);
    }*/

    holder.pos = position;

    //mFeedsListItemViewHolder.setData(allPersons.get(position));
    return convertView;
}

private class mHolder {
    TextView txt_suggestUserName;
    ImageView imgV_userProfile;
    Button btnFollow;
    Button btnFollowing;
    int pos;
}
 }

注意:我已经尝试过声明listview并在片段的onActivityCreated()方法中分配适配器但没有效果。 任何帮助将不胜感激。

0 个答案:

没有答案