主活动回收站视图上未设置行布局

时间:2016-06-30 10:03:38

标签: java android android-recyclerview android-cardview

我只是解析一些数据并显示在我的ListProperty类上。我使用RecyclerViewCardView作为行布局。不知怎的,一切都很好,我没有看到任何错误。但数据和行布局在我的主要活动中显示。活动显示空白。我正在尝试从几个小时,但亲切找到错误。请有人帮忙

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


            }
        });
    }
}

3 个答案:

答案 0 :(得分:1)

尝试删除父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>

希望它会有所帮助:)

答案 1 :(得分:1)

只需将notifyDataSetChanged()添加到Activity,如下所示:

    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);
                }
                //here notify your adapter
                //is to Notify any registered observers that the data set has changed.
                mAdapter.notifyDataSetChanged();

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


           }
       });

答案 2 :(得分:1)

因为在你的回调中你已经创建了列表的新实例

list = new ArrayList<Bean>();

然后实际上,适配器中新创建的列表和DataList指向2个不同的对象=&gt;在这里调用notifyDataSetChanged将无法正常工作。尝试在MyAdapter上编写方法updateData,并从回调方法中调用它

public void updateData(List<Bean> List){
   DataList = List;
   notifyDataSetChanged();
   }

在回调中:

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);
            }
            // update data
            mAdapter.updateData(list);

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


       }
   });