【问题标题】:Android CardView is not showing on activityAndroid CardView 未显示在活动中
【发布时间】:2017-05-01 19:50:45
【问题描述】:

我有一个卡片视图,它应该向用户显示来自我的 WebApi 的列表,我能够从我的 Api 中获取一个列表,但我认为问题是因为我试图在 AsyncTask 内设置一个适配器,我的代码:

我的适配器:

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;
    }
}

}

以及从我的 Api 接收数据的“活动”:

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();
}

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

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

还有布局:

<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>

还有 LayoutCard:

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="16dp"
        android:foregroundTint="@color/colorPrimary"
        android:layout_margin="8dp">
        <RelativeLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="@color/colorPrimary">
            <TextView
                android:id="@+id/image_card"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@style/TextAppearance.AppCompat.Title"
                android:textColor="@color/White"
                android:textSize="60sp"
                android:layout_centerInParent="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/price_card"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:textSize="18sp"
                android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                android:layout_alignParentBottom="true"
                android:layout_marginStart="16dp"
                android:layout_marginLeft="16dp"
                />

            <TextView
                android:id="@+id/district_card"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@style/TextAppearance.AppCompat.Title"
                android:textColor="#000000"
                android:textSize="16sp"
                android:layout_above="@+id/price_card"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp"/>

            <TextView
                android:id="@+id/date_card"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:textSize="9.5sp"
                android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                android:layout_alignBaseline="@+id/price_card"
                android:layout_alignBottom="@+id/price_card"
                android:layout_alignEnd="@+id/district_card"
                android:layout_alignRight="@+id/district_card"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

我的布局文件中的屏幕截图:

从我的手机:

谢谢!

【问题讨论】:

  • 您的卡片视图在布局中的哪个位置?
  • 布局文件夹内,查看编辑帖!
  • 尝试将您的卡片视图放入您的线性布局中
  • 这不是 cardview 问题,您没有获取数据。
  • 你应该在设置适配器之前Log.duserModelView.size()。

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


【解决方案1】:

myadapter.notifyItemInserted()

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

【讨论】:

  • 这不是必需的。 setAdapter 应该正确通知列表显示某些内容(如果有数据)
【解决方案2】:

调试此类问题我会做的是,附加一个调试器并使用断点/日志来确定是否:

  • 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);
    ....
}

【讨论】:

  • 我注意到,AdapterUserView adapterUserView 对象返回零,但它有来自我的 WebApi 的数据,我应该使用其他方式将其转换为列表吗?
  • userModelView 怎么样?它在使用解析数据初始化后是否包含任何元素?
  • 仍然无法正常工作:/ 我在关注this
  • getItemCount() 是否返回 1?
【解决方案3】:

你收到来自 web 服务的响应了吗???

那你初始化recycler view adapter的地方不对,就这样吧

在 onCreate 内部用这样的空数组列表初始化适配器

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 创建方法来获取您更新的列表并 notifyDataSetchanged 如下所示

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

现在在获得 web 服务响应后,调用您的适配器方法,并在 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;`

【讨论】:

  • 是的。我收到了!
  • 你得到的列表大小是多少?
  • 我得到的尺寸是 1
猜你喜欢
  • 1970-01-01
  • 2017-02-13
  • 2016-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 2015-02-04
  • 2012-11-10
相关资源
最近更新 更多