当我在List<OrderList>
中添加loadOrdersData()
任何项目时,会抛出此内容:
D / ViewGroup:已调用addInArray,这= android.support.v7.widget.RecyclerView {41cab8f0 VFED .... .F .... ID 0,0-480,770#7f0c0073 app:id / rv}调用堆栈= java.lang.Throwable中:addInArray在android.view.ViewGroup.addInArray(ViewGroup.java:3786)在android.view.ViewGroup.addViewInner(ViewGroup.java:3740)在android.view.ViewGroup.addView(ViewGroup中。 Java的:3564)在android.view.ViewGroup.addView(ViewGroup.java:3509)在android.support.v7.widget.RecyclerView $ 5.addView(RecyclerView.java:649)在android.support.v7.widget.ChildHelper。 addView(ChildHelper.java:107)在android.support.v7.widget.RecyclerView $ LayoutManager.addViewInt(RecyclerView.java:7115)在android.support.v7.widget.RecyclerView $ LayoutManager.addView(RecyclerView.java:7073)在android.support.v7.widget.RecyclerView $ LayoutManager.addView(RecyclerView.java:7061)在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1428)在android.support.v7.widget.LinearLayoutManager .fill伪(LinearLayoutManager.ja VA:1377)在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)在android.support.v7.widget .RecyclerView.dispatchLayout(RecyclerView.java:3124)在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3568)在android.view.View.layout(View.java:15125)在android.view。 ViewGroup.layout(ViewGroup.java:4862)在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1197)在android.view.View.layout(View.java:15125)在android.view.ViewGroup .layout(ViewGroup.java:4862)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)在android.widget.FrameLayout.onLayout(FrameLayout.java:450)在android.view.View.layout(查看。的java:15125)在android.view.ViewGroup.layout(ViewGroup.java:4862)在android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)在android.view.View.layout(View.java :15125)在android.vi ew.ViewGroup.layout(ViewGroup.java:4862)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)在android.widget.FrameLayout.onLayout(FrameLayout.java:450)在android.view.View.layout (View.java:15125)在android.view.ViewGroup.layout(ViewGroup.java:4862)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)在android.widget.LinearLayout.layoutVertical(LinearLayout.java: 1742)在android.widget.LinearLayout.onLayout(LinearLayout.java:1651)在android.view.View.layout(View.java:15125)在android.view.ViewGroup.layout(ViewGroup.java:4862)在机器人。 widget.FrameLayout.layoutChildren(FrameLayout.java:515)在android.widget.FrameLayout.onLayout(FrameLayout.java:450)在android.view.View.layout(View.java:15125)在android.view.ViewGroup.layout (ViewGroup.java:4862)在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323)在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java: 1192)在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6231)在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:788)在android.view.Choreographer.doCallbacks(Choreographer.java:591)在android.view.Choreographer.doFrame(Choreographer.java:560)在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:774)在android.os.Handler.handleCallback(Handler.java:808)在android.os .Handler.dispatchMessage(Handler.java:103)在android.os.Looper.loop(Looper.java:193)在android.app.ActivityThread.main(ActivityThread.java:5305)在java.lang.reflect.Method中。 invokeNative(本地方法)在java.lang.reflect.Method.invoke(Method.java:515)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:824)在com.android.internal。 os.ZygoteInit.main(ZygoteInit.java:640)at dalvik.system.NativeStart.main(Native Method)
我尝试在adapter.notifyDataSetChanged();
setAdapter()
之前添加:loadOrdersData()
,但这没有任何意义。有谁看到它是由什么造成的?
以下是代码:
MainActivity:
public class CourierActivity extends AppCompatActivity {
private TableLayout orderTable;
private RecyclerView recyclerView;
private RecycleAdapter adapter;
private List<OrderListItem> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.courier_main);
recyclerView = (RecyclerView) findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(CourierActivity.this));
adapter = new RecycleAdapter(listItems, getApplicationContext());
recyclerView.setAdapter(adapter);
loadOrdersData();
}
private void loadOrdersData() {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest sr = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
listItems = new ArrayList<>();
try {
String rsp = new String(response.getBytes("ISO-8859-1"), "UTF-8");
JSONArray orders = new JSONArray(rsp);
for (int i = 0; i < orders.length(); i++) {
OrderListItem listItem = new OrderListItem(
// ..
);
listItems.add(listItem);
}
adapter = new RecycleAdapter(listItems, getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
queue.add(sr);
}
}
OrderListItem:
public class OrderListItem {
private String name;
private String status;
private String price;
private String courier;
public OrderListItem(String name, String status, String price, String courier) {
this.name = name;
this.status = status;
this.price = price;
this.courier = courier;
}
public String getName() { return name; }
public String getStatus() { return status;}
public String getPrice() { return price; }
public String getCourier() { return courier; }
}
RecycleAdapter:
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
private List<OrderListItem> listItems;
private Context context;
public RecycleAdapter(List<OrderListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rv_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
OrderListItem listItem = listItems.get(position);
holder.TextViewName.setText(listItem.getName());
holder.TextViewStatus.setText(listItem.getStatus());
holder.TextViewPrice.setText(listItem.getPrice());
holder.TextViewCourier.setText(listItem.getCourier());
}
@Override
public int getItemCount() {
int count = 0;
if (listItems != null && !listItems.isEmpty()) {
count = listItems.size();
}
return count;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView TextViewName;
public TextView TextViewStatus;
public TextView TextViewPrice;
public TextView TextViewCourier;
public ViewHolder(View itemView) {
super(itemView);
TextViewName = (TextView) itemView.findViewById(R.id.orderListName);
TextViewStatus = (TextView) itemView.findViewById(R.id.orderListStatus);
TextViewPrice = (TextView) itemView.findViewById(R.id.orderListPrice);
TextViewCourier = (TextView) itemView.findViewById(R.id.orderListCourier);
}
}
}
@布局/ rv_item.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="wrap_content">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="@dimen/activity_horizontal_margin"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/orderListName"
android:text="Order name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/orderListStatus"
android:text="Status"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/orderListPrice"
android:text="Price"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/orderListCourier"
android:text="Courier"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
@布局/ main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.seether.myapplication.CourierActivity"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv"/>
</android.support.v4.widget.DrawerLayout>
修改即可。如果我将自定义数据输入listItems
,则无需日志警告即可完美运行。
答案 0 :(得分:1)
使用
adapter = new RecycleAdapter(listItems, CourierActivity.this);
而不是
adapter = new RecycleAdapter(listItems, getApplicationContext());
同时在RecyclerView
android:orientation="vertical"
更新此内容:
String rsp = new String(response.getBytes(), "UTF-8");
答案 1 :(得分:1)
试试这种方式
CourierActivity.java
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
private List<OrderListItem> listItems;
private Context context;
public RecycleAdapter(List<OrderListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
public setList(List<OrderListItem> listItems){
//add list to parent list and notify your adapter
this.listItems.addAll(listItems);
notifyDataSetChanged();
}
...
}
RecycleAdapter.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.seether.myapplication.CourierActivity"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv"/
</LinearLayout>
修改1:
main.xml中
public int getLastId() {
int _id = 0;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("calls", new String[] {BaseColumns._ID}, null, null, null, null, null);
if (cursor.moveToLast()) {
_id = cursor.getInt(0);
}
cursor.close();
db.close();
return _id;
}
答案 2 :(得分:1)
我做了几处修改并写了评论。试试吧,
public class CourierActivity extends AppCompatActivity {
private TableLayout orderTable;
private RecyclerView recyclerView;
private RecycleAdapter adapter;
//********* Initialize over here **********//
private List<OrderListItem> listItems = new ArrayList<>();
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.courier_main);
//********** Initialize *******//
mContext = CourierActivity.this;
recyclerView = (RecyclerView) findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(CourierActivity.this));
//********* added class context *********//
adapter = new RecycleAdapter(listItems, mContext);
recyclerView.setAdapter(adapter);
loadOrdersData();
}
private void loadOrdersData() {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest sr = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//******** Remove this line *************//
listItems = new ArrayList<>();
//******** Add this line ************//
listItems.clear();
try {
String rsp = new String(response.getBytes("ISO-8859-1"), "UTF-8");
JSONArray orders = new JSONArray(rsp);
for (int i = 0; i < orders.length(); i++) {
OrderListItem listItem = new OrderListItem(
// ..
);
listItems.add(listItem);
}
//*********Remove these both lines **********//
adapter = new RecycleAdapter(listItems, getApplicationContext());
recyclerView.setAdapter(adapter);
//************ Add this line **********//
adapter.notifyDataSetChanged();
} catch (JSONException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
queue.add(sr);
}
}
答案 3 :(得分:1)
Volley似乎异步获取数据,因此管理RecyclerView
的最合理方法是首先使用空(但不为空)List
和活动<设置适配器/ strong> context(例如Mainactivity.this
)。
当Volley最终得到所有元素时,你添加了所有元素(而不是for(int i = 0; i < orders.length(); i++)
你可以使用for(OrderListItem listItem : orders);
),你可以调用adapter.notifyDataSetChanged()
答案 4 :(得分:1)
而不是在ArrayList
中创建新的RecyclerAdapter
和onResponse()
实例,我更喜欢,
listItems
。 OrderListItem
个实例添加到listItems
。notifyDataSetChanged()
的{{1}}。例如:
adapter
并确保正确初始化@Override
public void onResponse(String response) {
listItems.clear();
try {
//loop and add all items to listItems here
adapter.notifyDataSetChanged();
} catch (JSONException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
,因为我在RecyclerAdapter
的{{1}}方法中看不到任何创建listItems
的代码。
onCreate()
答案 5 :(得分:1)
不要将列表对象从activity传递给适配器。而是在适配器中有一个单独的数组列表。 将适配器构造函数更改为:
public RecycleAdapter(List<OrderListItem> listItems, Context context) {
this.listItems = new ArrayList<>(listItems);
this.context = context;
}
正如Maddy建议在适配器中使用setMethod传递listitem数组并向其添加所有项目并调用notifyDataSetChanged。
我想这可以解决您在活动和适配器中不使用相同的listitems对象时的问题。