Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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 线性布局背景色不以编程方式更改_Android_Android Recyclerview_Android Linearlayout_Background Color - Fatal编程技术网

Android 线性布局背景色不以编程方式更改

Android 线性布局背景色不以编程方式更改,android,android-recyclerview,android-linearlayout,background-color,Android,Android Recyclerview,Android Linearlayout,Background Color,我正在为锁定和解锁时间创建活动日志,我需要根据锁定或解锁对背景颜色进行排序。然而,我似乎不能改变我的线性布局的背景色,无论我放什么。非常感谢您的帮助,谢谢 这是我的主要活动 public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private RecyclerView.Adapter adapter; private List<ListItem> listI

我正在为锁定和解锁时间创建活动日志,我需要根据锁定或解锁对背景颜色进行排序。然而,我似乎不能改变我的线性布局的背景色,无论我放什么。非常感谢您的帮助,谢谢

这是我的主要活动

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;

private List<ListItem> listItems;

DatabaseReference database;
@Override
protected void onCreate(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
    View view = inflater.inflate(R.layout.list_item, null);
    final LinearLayout layout= (LinearLayout) view.findViewById(R.id.damsi);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    // SharedPreferences preferences=getSharedPreferences(LOCK_PREFS,MODE_PRIVATE);
    database = FirebaseDatabase.getInstance().getReference("Activity Log/device321");

    listItems = new ArrayList<>();

    database.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot usersnapshot : dataSnapshot.getChildren()) {
                Map<String, String> map = (Map<String, String>) usersnapshot.getValue();
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    if (entry.getKey().equals("LockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
                    if (entry.getKey().equals("UnlockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
                    listItems.add(new ListItem(entry.getKey(), entry.getValue()));
                }


                //   ListItem listItem = usersnapshot.getValue(ListItem.class);
            }
            recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
            adapter = new MyAdapter(listItems, MainActivity.this);
            recyclerView.setAdapter(adapter);
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
  }
}
public类MainActivity扩展了AppCompatActivity{
私人回收站;
专用循环视图适配器;
私人物品清单;
数据库参考数据库;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
LayoutFlater充气器=LayoutFlater.from(getApplicationContext());
视图=充气机。充气(R.layout.list_项,空);
最终LinearLayout布局=(LinearLayout)视图.findViewById(R.id.damsi);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(recyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
//SharedReferences首选项=GetSharedReferences(锁定首选项,模式专用项);
database=FirebaseDatabase.getInstance().getReference(“活动日志/device321”);
listItems=new ArrayList();
database.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
对于(DataSnapshot usersnapshot:DataSnapshot.getChildren()){
Map Map=(Map)usersnapshot.getValue();
对于(Map.Entry:Map.entrySet()){
if(entry.getKey().equals(“锁定时间”)){
System.out.println(entry.getKey());
布局.setBackgroundColor(Color.parseColor(#FFB71616”);
}
if(entry.getKey().equals(“UnlockTime”)){
System.out.println(entry.getKey());
布局.setBackgroundColor(Color.parseColor(#FFB71616”);
}
添加(新的ListItem(entry.getKey(),entry.getValue());
}
//ListItem ListItem=usersnapshot.getValue(ListItem.class);
}
setLayoutManager(新的LinearLayoutManager(MainActivity.this));
adapter=新的MyAdapter(listItems,MainActivity.this);
recyclerView.setAdapter(适配器);
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
}
}
这是我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.CardView

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin">

    <LinearLayout
        android:id="@+id/damsi"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#baa40f"
        android:orientation="vertical"
        android:padding="16dp"
        android:visibility="visible">

        <TextView
            android:id="@+id/textViewHead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:text="Heading"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/textViewDesc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:text="Description" />
    </LinearLayout>

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

</LinearLayout>

MyAdapter

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

private List<ListItem> listItems;
private Context context;

public MyAdapter(List<ListItem> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    ListItem listItem = listItems.get(position);


    holder.textViewHead.setText(listItem.getHead());
    holder.textViewDesc.setText(listItem.getDesc());

}

@Override
public int getItemCount() {
    return listItems.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public LinearLayout layout;
    public TextView textViewHead;
    public TextView textViewDesc;


    public ViewHolder(View itemView) {
        super(itemView);

        textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
        textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);

    }
}


}
公共类MyAdapter扩展了RecyclerView.Adapter{ 私人物品清单; 私人语境; 公共MyAdapter(列表项、上下文){ this.listItems=listItems; this.context=上下文; } @非空 @凌驾 public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){ 视图v=LayoutInflater.from(parent.getContext()) .充气(R.layout.list_项,父项,假); 返回新的视图持有者(v); } @凌驾 public void onBindViewHolder(@NonNull ViewHolder,int位置){ ListItem ListItem=listItems.get(位置); holder.textViewHead.setText(listItem.getHead()); holder.textViewDesc.setText(listItem.getDesc()); } @凌驾 public int getItemCount(){ 返回listItems.size(); } 公共类ViewHolder扩展了RecyclerView.ViewHolder{ 公共线路布局; 公共文本视图文本视图头; 公共文本视图文本视图描述; 公共视图持有者(视图项视图){ 超级(项目视图); textViewHead=(TextView)itemView.findViewById(R.id.textViewHead); textViewDesc=(TextView)itemView.findViewById(R.id.textViewDesc); } } }
从MainActivity中删除此代码

 LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        View view = inflater.inflate(R.layout.list_item, null);
        final LinearLayout layout= (LinearLayout) view.findViewById(R.id.damsi);
从OnDataChangedMethod中删除这些行

if (entry.getKey().equals("LockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
                    if (entry.getKey().equals("UnlockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
在ViewHolder类中,在ViewHolder构造函数中添加这些行

layout= (LinearLayout) view.findViewById(R.id.damsi);
在onBindViewHolder方法中添加这些行

String key = listItem.getHead();
if(key.equals("LockTime"))
{
    System.out.println(entry.getKey());
    layout.setBackgroundColor(Color.parseColor("#FFB71616"));
}
if(key..equals("UnlockTime")){
    System.out.println(entry.getKey());
    layout.setBackgroundColor(Color.parseColor("#FF001600"));
    //I changed the color as both were having same hexcodes :P
}
holder.textViewHead.setText(listItem.getHead());
holder.textViewDesc.setText(listItem.getDesc());

从MainActivity中删除此代码

 LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        View view = inflater.inflate(R.layout.list_item, null);
        final LinearLayout layout= (LinearLayout) view.findViewById(R.id.damsi);
从OnDataChangedMethod中删除这些行

if (entry.getKey().equals("LockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
                    if (entry.getKey().equals("UnlockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
在ViewHolder类中,在ViewHolder构造函数中添加这些行

layout= (LinearLayout) view.findViewById(R.id.damsi);
在onBindViewHolder方法中添加这些行

String key = listItem.getHead();
if(key.equals("LockTime"))
{
    System.out.println(entry.getKey());
    layout.setBackgroundColor(Color.parseColor("#FFB71616"));
}
if(key..equals("UnlockTime")){
    System.out.println(entry.getKey());
    layout.setBackgroundColor(Color.parseColor("#FF001600"));
    //I changed the color as both were having same hexcodes :P
}
holder.textViewHead.setText(listItem.getHead());
holder.textViewDesc.setText(listItem.getDesc());

您得到的确切输出是什么?我得到的输出是我在XML文档中添加的android背景色。我尝试删除XML背景色,但仍然没有成功。我无法更改列表项XML文档中的任何内容。问题出在我的布局充气机上了吗?你能从线性布局上拆下最后一个吗。不走运。我只是想不出问题,将线性布局声明为实例变量你得到了什么样的精确输出??我得到的输出是我在XML文档中添加的android背景色。我尝试删除XML背景色,但仍然没有成功。我无法更改列表项XML文档中的任何内容。问题出在我的布局充气机上了吗?你能从线性布局上拆下最后一个吗。不走运。我只是想不出问题,将线性布局声明为实例变量我有点怀疑,为什么我无法更改主活动中的任何内容?onBindViewHolder方法是将列表项绑定到xml以供回收器查看的地方。在主活动期间,对acces线性布局的引用不会产生任何影响,因为onBindViewHolder会重新绘制布局和数据我有一点怀疑,为什么我无法更改主活动中的任何内容?onBindViewHolder方法是将列表项绑定到xml以供回收器查看的位置。在主活动期间,引用acces线性布局不会产生任何影响,因为onBindViewHolder会重新绘制布局和数据