数据未显示在列表中

时间:2018-06-08 09:09:06

标签: android

我正在尝试将数据设置到我的recyclerView中,而我无法将数据设置为列表。当我调试时,它执行语句,并在logcat中找到值。但是当我跑步时,在列表中找不到它。我已经尝试了很多。谁能请帮忙。这就是我所做的。

适配器1

SelectedAdditionalItemsAdapter adapter = new SelectedAdditionalItemsAdapter(activity, row.getAdditionalItems(), row);
                recyclerView.setAdapter(adapter);
         adapter.setData(row.getAdditionalItems());

SelectedAdditionalItemsAdapter

public class SelectedAdditionalItemsAdapter extends RecyclerView.Adapter<SelectedAdditionalItemsAdapter.ViewHolder> {

private List<Product> rows = new ArrayList<>();

        Context context;
private OrderItem orderItem;


private Activity activity;

public OnOrderItemDeleteListner onOrderItemDeleteListner;



public SelectedAdditionalItemsAdapter(Context context,List<Product> rows,OrderItem orderItem) {
        this.context = context;
        this.activity = (Activity) context;
        this.rows=rows;
        this.orderItem=orderItem;
        }

public SelectedAdditionalItemsAdapter(){

        }

public Product getItem(int position) {
        try {
        return rows.get(position);
        } catch (Exception e) {
        return null;
        }
        }

public void setData(List<Product> rows) {
        this.rows = rows;
        notifyDataSetChanged();

        }

@Override
public long getItemId(int position) {
        return position;
        }


@Override
public SelectedAdditionalItemsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {


        return new SelectedAdditionalItemsAdapter.ViewHolder(LayoutInflater.from(parent.getContext())
        .inflate(R.layout.additional_item_cell, parent, false));
        }

@Override
public void onBindViewHolder(SelectedAdditionalItemsAdapter.ViewHolder holder, int position) {

        Product row = getItem(position);

        holder.bindVehicle(row, position);

        }

@Override
public int getItemCount() {
        try {
        return rows.size();
        } catch (NullPointerException e) {
        return 0;
        }
        }

private void removeItem(int position) {
        try {
        this.rows.remove(position);
        }catch (Exception e) {

        }
        notifyDataSetChanged();
        }

class ViewHolder extends RecyclerView.ViewHolder  {

    TextView title, remarkView, quantityView, priceView;
    ImageView deleteButton;
    CircularNetworkImageView imageView;
    private Product row;

    ViewHolder(View view) {
        super(view);

        title = (TextView) view.findViewById(R.id.title_view);
        remarkView = (TextView) view.findViewById(R.id.remark_view);
        imageView = (CircularNetworkImageView) view.findViewById(R.id.image_view);
        quantityView = (TextView) view.findViewById(R.id.quantity_view);
        priceView = (TextView) view.findViewById(R.id.price_view);
        deleteButton = (ImageView) view.findViewById(R.id.delete_button);

    }
        public void bindVehicle(final Product row, final int position) {

        deleteButton.setTag(position);///added


        this.row = row;
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                         orderItem.removeProduct(row);
                    removeItem(position);
                    notifyDataSetChanged();



            }

        });

        try {
            this.title.setText(row.getName());
            if(row.getDescription() != null && row.getDescription().length() > 0) {
                remarkView.setText(row.getDescription());
                remarkView.setVisibility(View.VISIBLE);
            }else{
                remarkView.setVisibility(View.GONE);
            }
            String title = row.getName();
            imageView.setImageUrl(null, RequestQueue.getImageLoader());
            ColorGenerator generator = ColorGenerator.MATERIAL;
            int color = generator.getColor(title);

            try {
                if(title.length() > 0) {
                    TextDrawable drawable2 = TextDrawable.builder()
                            .buildRound(title.substring(0, 1), color);
                    imageView.setImageDrawable(drawable2);
                }
            }catch (NullPointerException e) {
          }
quantityView.setText(Integer.toString((int)row.getQuantity()));
            priceView.setText(row.getPriceForUnitToDisplay());    
        } catch (NullPointerException e) {
            e.printStackTrace();
        }}}

public interface OnOrderItemDeleteListner {
    void onDeleteItem();
}}

This is my 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:background="@drawable/layout_border">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp"
            android:background="@color/white">

            <driver.codepoints.com.driver.utils.CircularNetworkImageView
                android:id="@+id/image_view"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:scaleType="fitXY"
                android:layout_marginRight="15dp"/>
            <com.devspark.robototextview.widget.RobotoTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/title_view"
                android:textSize="18sp"
                android:textColor="@color/black" />


            <ImageView
                android:id="@+id/delete_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:tint="@color/android_red"
                android:padding="8dp"
                android:clickable="true"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:src="@drawable/ic_delete_black_24dp"/>


        </LinearLayout>

        <com.devspark.robototextview.widget.RobotoTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:id="@+id/remark_view"
            android:textSize="14sp" />
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="50dp"
                android:gravity="center_vertical"
                android:orientation="horizontal">
                <com.devspark.robototextview.widget.RobotoTextView
                    android:id="@+id/quantity_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/accent_color"
                    android:gravity="center"
                    android:paddingBottom="2dp"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:paddingTop="2dp"
                    android:textColor="@color/white"
                    android:textSize="16sp"
                     />
                <com.devspark.robototextview.widget.RobotoTextView
                    android:id="@+id/price_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/login_text_hint_color"
                    android:gravity="center"
                    android:paddingBottom="2dp"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:paddingTop="2dp"
                    android:textColor="@color/black"
                    android:textSize="16sp"
                     />    
            </LinearLayout>    
        </LinearLayout>

1 个答案:

答案 0 :(得分:1)

如果context用于充气布局,请使用parent.getContext()

更改此

 return new SelectedAdditionalItemsAdapter.ViewHolder(LayoutInflater.from(parent.getContext())
        .inflate(R.layout.additional_item_cell, parent, false));

 return new SelectedAdditionalItemsAdapter.ViewHolder(LayoutInflater.from(context)
        .inflate(R.layout.additional_item_cell, parent, false));

还将layoutManager设置为RecyclerView

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(mLayoutManager);

希望这会有所帮助。