Android CardView未在活动

时间:2016-12-16 04:19:43

标签: android android-layout android-asynctask android-cardview

我有一个cardview应该向用户显示来自我的WebApi的列表,我能够从我的Api获得一个列表,但我认为问题是因为我试图在{内部设置适配器{1}},我的代码:

MyAdapter:

AsyncTask

}

"活动"从我的Api接收数据:

public class AdapterUserView extends RecyclerView.Adapter<AdapterUserView.UserViewHolder> {

private List<UserModelView> mList;
private LayoutInflater mLayoutInflater;
private Context mContext;

public AdapterUserView(Context c,List<UserModelView> l){
    mContext = c;
    mList = l;
    mLayoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mLayoutInflater.inflate(R.layout.layout_user_card,parent,false);
    AdapterUserView.UserViewHolder mainViewHolder = new AdapterUserView.UserViewHolder(view);
    return mainViewHolder;
}

@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
    holder.txtDistrict.setText(mList.get(position).getDistric());
    holder.txtPrice.setText(mList.get(position).getAds_price());
    holder.txtDate.setText(mList.get(position).getAds_date());
    holder.txtImage.setText(mList.get(position).getImage1());
}
@Override
public int getItemCount() {
    return mList.size();
}

public void addListItem(UserModelView userModelView, int position){
    mList.add(userModelView);
    notifyItemInserted(position);
}

public class UserViewHolder extends RecyclerView.ViewHolder {
    public TextView txtDistrict;
    public TextView txtPrice;
    public TextView txtDate;
    public TextView txtImage;
    public View view;
    public UserViewHolder(View itemView) {
        super(itemView);
        txtDistrict = (TextView)itemView.findViewById(R.id.district_card);
        txtPrice = (TextView)itemView.findViewById(R.id.price_card);
        txtDate = (TextView)itemView.findViewById(R.id.date_card);
        txtImage = (TextView)itemView.findViewById(R.id.image_card);
        view = itemView;
    }
}

我试图将我的列表序列化为CardView,并在onCreate之后:

public void getAdsUser(final Activity context){
    new AsyncTask<Void, Void, String>() {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {
                Response = new WebBase().getUserAds(context);
                if(Response.equals("NODATA")){
                    return "NODATA";
                }
                if (Response.equals("EMPTY")){
                    return "EMPTY";
                }
                if (Response.isEmpty()){
                    return "ERROR";
                }
                else {
                    return "OK";
                }
            } catch (IOException e) {
                e.printStackTrace();
                return "ERROR";
            }
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            switch (s){
                case "NODATA":
                    GenericAlertDialog.MakeDialog(UserAds.this,R.string.NODATA);
                    break;
                case "ERROR":
                    GenericAlertDialog.MakeDialog(UserAds.this,R.string.Error_Internet);
                    break;
                case "EMPTY":
                    GenericAlertDialog.MakeDialog(UserAds.this,R.string.No_ads_found);
                    break;
                case "OK":
                    Type listType = new TypeToken<ArrayList<UserModelView>>(){}.getType();
                    List<UserModelView> userModelView = new Gson().fromJson(Response,listType);
                    AdapterUserView adapterUserView = new AdapterUserView(UserAds.this,userModelView);
                    recyclerView.setAdapter(adapterUserView);
                    break;
            }
        }
    }.execute();
}

布局:

recyclerView = (RecyclerView) findViewById(R.id.myRecycler);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    getAdsUser(UserAds.this);

和LayoutCard:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/myRecycler"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

我的带有cardview的布局文件的屏幕截图:

enter image description here

和我的手机:

enter image description here

谢谢!

3 个答案:

答案 0 :(得分:0)

myadapter.notifyItemInserted()

我认为你必须通知postExecute()。 每当您对列表进行更改时,必须通知刷新视图。

答案 1 :(得分:0)

我要做的是调试这样的问题,附上一个调试器并使用断点/日志来查明是否:

    在调用适配器的构造函数之前,
  • Response变量已初始化
  • userModelView在解析响应后包含多个项目
  • 正在自动调用适配器中的
  • getItemCount()并返回大于零的值
  • onBindViewHolder被称为

修复它的步骤会有很大的不同,具体取决于您发现上述检查失败的位置。

我建议不要为Response使用字段/实例变量,而是从doInBackground返回它以在onPostExecute中使用它。此类实例变量往往会引入难以调试/处理/修复的错误。

修改

尝试更改:

public AdapterUserView(Context c,List<UserModelView> l){
    mContext = c;
    mList = l;
    ....
}

为:

public AdapterUserView(Context c,List<UserModelView> l){
    mContext = c;

    mList = new ArrayList<>();
    mList.addAll(l);
    ....
}

答案 2 :(得分:0)

您是否收到了来自webservice ???的响应?

然后,你初步使用Recycler视图适配器的地方不正确,所以就像下面这样做

在onCreate中使用像这样的空arraylist初始化适配器

recyclerView = (RecyclerView) findViewById(R.id.myRecycler);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
AdapterUserView adapterUserView = new AdapterUserView(UserAds.this,new ArrayList<UserModelView>();
recyclerView.setAdapter(adapterUserView);

然后使用AdapterUserViewClass创建方法来获取你的upated列表和notifyDataSetchanged,如下所示

public setData(List<UserModelView> l){
mList = l;
notifyDataSetChanged();}

现在在获得webservice响应后,使用onPostExecute方法中的更新列表调用适配器方法,如下所示

case "OK":
                Type listType = new TypeToken<ArrayList<UserModelView>>(){}.getType();
                List<UserModelView> userModelView = new Gson().fromJson(Response,listType);
                if (userModelView != null && userModelView.size > 0)
                adapterUserView.setData(userModelView);

                break;`