java - 主 Activity 回收器 View 上未设置行布局

标签 java android android-recyclerview android-cardview

我只是解析一些数据并显示在我的 ListProperty 类上。我使用 RecyclerViewCardView 作为行布局。不知怎的,一切都工作正常,我没有看到任何详细的错误。但数据和行布局没有显示在我的主要 Activity 中。 Activity 显示为空白。我正在尝试几个小时,但发现了错误。请有人帮忙

我的MainActivity.xml

<RelativeLayout 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/cardList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

这是 row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageViewFront" />
            </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_marginLeft="100dp"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="New Text"
            android:textColor="#0055CC"
            android:id="@+id/textViewPropertyname" />

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:textColor="#0055CC"
            android:layout_columnWeight="1"
            android:id="@+id/textViewPropertyDescription"/>
    </LinearLayout>

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


</LinearLayout>

这是 myAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
List<Bean> DataList;
public MyAdapter(List<Bean> List){
    super();
    DataList = List;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView propertyName;
    public TextView propertyDesc;
    public ImageView ImageFront;
    public MyViewHolder(View v) {
        super(v);
        propertyName = (TextView) v.findViewById(R.id.textViewPropertyname);
        propertyDesc = (TextView) v.findViewById(R.id.textViewPropertyDescription);
        ImageFront  = (ImageView) v.findViewById(R.id.imageViewFront);
    }
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext()).
            inflate(R.layout.list_card_view,parent,false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
    Bean bean = DataList.get(position);
    holder.propertyName.setText(bean.getPropertyName());
    holder.propertyDesc.setText(bean.getPropertyDescription());
    holder.ImageFront.setImageResource(R.drawable.agriculture);

}

@Override
public int getItemCount() {
    return DataList.size();
}

}

这是我的 MainActivity.java

    public class ListProperty extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private MyAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    Bean bean;
    AQuery aQuery;
    JSONObject jsonObject;
    List<Bean> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_property);
        aQuery= new AQuery(getApplicationContext());
        String URL = "http://192.168.0.107/propertyapp/service/simpleservice.php";
        list = new ArrayList<Bean>();
        mRecyclerView = (RecyclerView) findViewById(R.id.cardList);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyAdapter(list);
        mRecyclerView.setAdapter(mAdapter);
        aQuery.ajax(URL, JSONObject.class,new AjaxCallback<JSONObject>(){
            @Override
            public void callback(String url, JSONObject object, AjaxStatus status) {

                try {
                    bean = new Bean();
                    list = new ArrayList<Bean>();
                    String string = object.getString("data");
                    JSONArray jsonArray = new JSONArray(string);
                    for (int i=0;i<jsonArray.length();i++) {
                        jsonObject = jsonArray.getJSONObject(i);
                        bean.setPropertyName("property_name");
                        bean.setPropertyDescription("property_desc");
                        list.add(bean);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });
    }
}

最佳答案

尝试删除父 LinearLayout。因为你有 CardView 充当列表项。

试试这个 row.xml -

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/imageViewFront" />
                </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_marginLeft="100dp"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:text="New Text"
                android:textColor="#0055CC"
                android:id="@+id/textViewPropertyname" />

            <TextView
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:textColor="#0055CC"
                android:layout_columnWeight="1"
                android:id="@+id/textViewPropertyDescription"/>
        </LinearLayout>

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

希望对你有帮助:)

关于java - 主 Activity 回收器 View 上未设置行布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38119908/

相关文章:

java - 从命令行运行 java,文件夹结构和包名称不匹配

Java:向边框布局添加菜单栏

java - 在每个“回收者” View 中添加自定义布局的问题

java - 尝试遵循之前关于如何创建动画的答案,但它完全消失了

java - JOptionPane 打开时是否可以暂停当前线程?

java - ListView onScrollListener 不起作用

android - 如何使用 Retrofit 在 RxJava 中管理结果

java - 使用本教程尝试为 Android 创建 Twitter 应用程序

android - 如何在 Fragment 中实现 recyclerview onClick?

android - 水平recyclerview内部垂直recyclerview滚动性能