gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:26:07 26 4
gpt4 key购买 nike

我只是解析一些数据并显示在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com