Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在每个不同的cardview中更改指示灯颜色?_Android_Android Cardview - Fatal编程技术网

Android 如何在每个不同的cardview中更改指示灯颜色?

Android 如何在每个不同的cardview中更改指示灯颜色?,android,android-cardview,Android,Android Cardview,我创建了一个cardview设计,指示器位于左侧(红色、绿色),但我有一个问题,如何更改指示器的颜色取决于数据库中的状态。如果状态为“否”,则指示灯变为红色,如果为“是”,则指示灯变为绿色 这是xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_widt

我创建了一个cardview设计,指示器位于左侧(红色、绿色),但我有一个问题,如何更改指示器的颜色取决于数据库中的状态。如果状态为“否”,则指示灯变为红色,如果为“是”,则指示灯变为绿色

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="6dp"
        card_view:cardElevation="3dp"
        card_view:cardUseCompatPadding="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.9">

                <ImageView
                    android:layout_marginLeft="22dp"
                    android:layout_marginTop="14dp"
                    android:layout_marginBottom="14dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/rectangle"/>

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.1"
                android:orientation="vertical">

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/layout_card_item"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:orientation="vertical"
                    android:padding="5dp">


                    <TextView
                        android:id="@+id/txt_order_date"
                        android:textSize="13sp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>

                    <TextView
                        android:id="@+id/txt_customer_name"

                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:id="@+id/txt_order_number"
                        android:textSize="12sp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>

                    <TextView
                        android:id="@+id/txt_product_status"
                        android:textSize="12sp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:id="@+id/txt_notes"
                        android:textSize="12sp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="false"
                        android:layout_weight="1"
                        android:maxLines="4"/>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:background="@drawable/layout_bg"
                        android:descendantFocusability="blocksDescendants"
                        android:orientation="horizontal"
                        android:layout_below="@+id/layout_card_item"
                        android:gravity="center"
                        android:weightSum="1">

                        <Button
                            android:id="@+id/download_pdf"
                            android:textSize="14sp"
                            android:layout_width="wrap_content"
                            android:layout_height="30dp"
                            android:background="#e1e3e8"
                            android:focusable="false"
                            android:textColor="#000307"
                            android:focusableInTouchMode="true"
                            android:drawableLeft="@drawable/ic_pictogram_download_orange"
                            android:text="  Download PDF"
                             />
                        <!--<ImageButton-->
                            <!--android:layout_width="10dp"-->
                            <!--android:layout_height="10dp"-->
                            <!--android:src="@mipmap/ic_order_download_pdf"/>-->


                    </LinearLayout>


                </LinearLayout>



            </LinearLayout>
        </LinearLayout>



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

这是适配器:

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder> {

    private ArrayList<OrderListModel> itemOrderList;

    public OrderListAdapter(ArrayList<OrderListModel> itemOrderList) {
        this.itemOrderList = itemOrderList;
    }

    @Override
    public OrderListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.order_list_item, parent, false);
        return new OrderListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderListViewHolder orderListViewHolder, int position) {
        orderListViewHolder.txtDate.setText(itemOrderList.get(position).getDate());
        orderListViewHolder.txtOrderNumber.setText(itemOrderList.get(position).getOrderNumber());
        orderListViewHolder.txtCustomerName.setText(itemOrderList.get(position).getCustomerName());
        orderListViewHolder.txtProductStatus.setText(itemOrderList.get(position).getProductStatus());
        orderListViewHolder.txtNotes.setText(itemOrderList.get(position).getNotes());
    }

    @Override
    public int getItemCount() {
        return (itemOrderList != null) ? itemOrderList.size() : 0;
//        return itemOrderList.size();
    }

    public class OrderListViewHolder extends RecyclerView.ViewHolder{
        private TextView txtDate, txtOrderNumber, txtCustomerName, txtProductStatus,
                txtNotes;

        public OrderListViewHolder(View itemView) {
            super(itemView);
            txtDate = (TextView) itemView.findViewById(R.id.txt_order_date);
            txtOrderNumber = (TextView) itemView.findViewById(R.id.txt_order_number);
            txtCustomerName = (TextView) itemView.findViewById(R.id.txt_customer_name);
            txtProductStatus = (TextView) itemView.findViewById(R.id.txt_product_status);
            txtNotes = (TextView) itemView.findViewById(R.id.txt_notes);
        }
    }
}
公共类OrderListAdapter扩展了RecyclerView.Adapter{ 私有ArrayList itemOrderList; public OrderListAdapter(ArrayList itemOrderList){ this.itemOrderList=itemOrderList; } @凌驾 public OrderListViewHolder onCreateViewHolder(视图组父级,int-viewType){ LayoutInflater LayoutInflater=LayoutInflater.from(parent.getContext()); 视图=布局更平坦。充气(R.layout.order\u list\u项,父项,false); 返回新的OrderListViewHolder(视图); } @凌驾 BindViewHolder上的公共无效(OrderListViewHolder OrderListViewHolder,int位置){ orderListViewHolder.txtDate.setText(itemOrderList.get(position.getDate()); orderListViewHolder.txtOrderNumber.setText(itemOrderList.get(position.getOrderNumber()); orderListViewHolder.txtCustomerName.setText(itemOrderList.get(position.getCustomerName()); orderListViewHolder.txtProductStatus.setText(itemOrderList.get(position.getProductStatus()); orderListViewHolder.txtNotes.setText(itemOrderList.get(position.getNotes()); } @凌驾 public int getItemCount(){ return(itemOrderList!=null)?itemOrderList.size():0; //返回itemOrderList.size(); } 公共类OrderListViewHolder扩展了RecyclerView.ViewHolder{ 私有文本视图txtDate、txtOrderNumber、txtCustomerName、txtProductStatus、, TXT注释; public OrderListViewHolder(查看项目视图){ 超级(项目视图); txtDate=(TextView)itemView.findViewById(R.id.txt\u order\u date); txtOrderNumber=(TextView)itemView.findViewById(R.id.txt\u order\u number); txtCustomerName=(TextView)itemView.findViewById(R.id.txt\u customer\u name); txtProductStatus=(TextView)itemView.findViewById(R.id.txt\u product\u status); txtNotes=(TextView)itemView.findViewById(R.id.txt\u notes); } } } 这是主要问题:

public class OrderListFragment extends Fragment {

    private RecyclerView recyclerView;
    private OrderListAdapter adapter;
    private ArrayList<OrderListModel> OrderListArrayList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_list, container, false);
        getActivity().setTitle(R.string.title_mn_order_list);
        addData();

        recyclerView = (RecyclerView) view.findViewById(R.id.orderList_recycler_view);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
        recyclerView.setLayoutManager(layoutManager);

        adapter = new OrderListAdapter(OrderListArrayList);
        recyclerView.setAdapter(adapter);
        return view;

    }

    void addData(){
        OrderListArrayList = new ArrayList<>();
        OrderListArrayList.add(new OrderListModel(
                "4 Juli 2018 10:54",
                "0001",
                "Jopa",
                "No",
                "Notes"));
        OrderListArrayList.add(new OrderListModel(
                "4 Juli 2018 10:54",
                "0001",
                "Jopa",
                "Yes",
                "Notes"));

    }
}
公共类OrderListFragment扩展了片段{
私人回收站;
私有OrderListAdapter;
私有ArrayList OrderListArrayList;
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u order\u list,container,false);
getActivity().setTitle(R.string.title\u mn\u order\u list);
addData();
recyclerView=(recyclerView)视图.findViewById(R.id.orderList\u recycler\u视图);
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(layoutManager);
适配器=新的OrderListAdapter(OrderListArrayList);
recyclerView.setAdapter(适配器);
返回视图;
}
void addData(){
OrderListArrayList=新建ArrayList();
添加(新的OrderListModel(
“4 Juli 2018 10:54”,
"0001",
“约帕”,
“不”,
“附注”);
添加(新的OrderListModel(
“4 Juli 2018 10:54”,
"0001",
“约帕”,
“是的”,
“附注”);
}
}

您知道如何更改它吗?

假设您的图像视图是您的指示栏:

<ImageView
android:id="@+id/ivIndicator" <!-- Add this id -->
android:layout_marginLeft="22dp"
android:layout_marginTop="14dp"
android:layout_marginBottom="14dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rectangle"/>
然后在onBindViewHolder中为imageview着色:

ivIndicator = (ImageView) itemView.findViewById(R.id.ivIndicator);
if("Yes".equals(itemOrderList.get(position).getStatus())) {
    orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.green))
else {
    orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.red))
}

假设您的ImageView是您的指示条:

<ImageView
android:id="@+id/ivIndicator" <!-- Add this id -->
android:layout_marginLeft="22dp"
android:layout_marginTop="14dp"
android:layout_marginBottom="14dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rectangle"/>
然后在onBindViewHolder中为imageview着色:

ivIndicator = (ImageView) itemView.findViewById(R.id.ivIndicator);
if("Yes".equals(itemOrderList.get(position).getStatus())) {
    orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.green))
else {
    orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.red))
}

如果要根据状态更改颜色,请在回收器适配器的onBindViewHolder上检查状态并设置为所需颜色

if(status.equals("Yes")
{
  rectangleImageView.setColor(Color.GREEN);
}
else
{
  rectangleImageView.setColor(Color.RED);
}

如果要根据状态更改颜色,请在回收器适配器的onBindViewHolder上检查状态并设置为所需颜色

if(status.equals("Yes")
{
  rectangleImageView.setColor(Color.GREEN);
}
else
{
  rectangleImageView.setColor(Color.RED);
}

制作两个可绘制文件,如@drawable/rectangle,并将其设置为所需的不同颜色

if(status.equals("No") {  
    yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithRed));
}

else if (status.equals("Yes") {
    yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithGreen);
}

制作两个可绘制文件,如@drawable/rectangle,并将其设置为所需的不同颜色

if(status.equals("No") {  
    yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithRed));
}

else if (status.equals("Yes") {
    yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithGreen);
}
  • 您必须为您的
    ImageView
    提供
    id
    ,比如
    状态
  • OrderListViewHolder
    中添加声明
    private ImageView状态
  • 在OrderListViewHolder中添加
    status=(ImageView)itemView.findViewById(R.id.status)
  • OrderListViewHolder中
    add

    if (itemOrderList.get(position).getStatus().equals("Yes")) {
        orderListViewHolder.status.setBackgroundColor(0x00ff00);
    } else { 
        orderListViewHolder.status.setBackgroundColor(0xff0000); }
    
  • 当然,您必须像所有其他方法一样创建
    getStatus()
    方法
    getSomething()

  • 您必须为您的
    ImageView
    提供
    id
    ,比如
    状态
  • OrderListViewHolder
    中添加声明
    private ImageView状态
  • 在OrderListViewHolder中添加
    status=(ImageView)itemView.findViewById(R.id.status)
  • OrderListViewHolder中
    add

    if (itemOrderList.get(position).getStatus().equals("Yes")) {
        orderListViewHolder.status.setBackgroundColor(0x00ff00);
    } else { 
        orderListViewHolder.status.setBackgroundColor(0xff0000); }
    

  • 当然,您必须像所有其他方法一样创建
    getStatus()
    方法
    getSomething()

    发布您的java代码。@Raj done,您有什么想法吗?发布您的java代码。@Raj done,您有什么想法吗?i g