默认布局也显示在listview中

时间:2018-06-17 18:05:29

标签: java android list

当我通过制作锁定和解锁时间的活动日志进行编码时,我遇到了一个我无法解决的非常不寻常的问题。活动日志显示正常,但也会显示默认的线性布局。我不明白为什么。任何人都可以随时提供帮助

这是我的ActivityLog活动

    package com.smartLock.sujitha.boltSmartLock;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.ListView;

   import com.google.firebase.database.DataSnapshot;
   import com.google.firebase.database.DatabaseError;
   import com.google.firebase.database.DatabaseReference;
   import com.google.firebase.database.FirebaseDatabase;
   import com.google.firebase.database.ValueEventListener;
   import com.smartLock.sujitha.boltSmartLock.Pack.ListItem;

  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;

  public class ActivityLog extends AppCompatActivity {


private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;

private List<ListItem> listItems;

SwipeRefreshLayout swipe;

DatabaseReference database;
LinearLayout lLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycle);
    recyclerView.setHasFixedSize(true);
    // SharedPreferences preferences=getSharedPreferences(LOCK_PREFS,MODE_PRIVATE);
    database = FirebaseDatabase.getInstance().getReference("Activity Log/device321");
    swipe = findViewById(R.id.swiper);
    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()) {
                    listItems.add(new ListItem(entry.getKey(), entry.getValue(),usersnapshot.child("Time").getValue().toString()
                    ));
                }
            }
            recyclerView.setAdapter(new MyAdapter(getApplicationContext(),listItems));
            adapter = new MyAdapter(listItems, ActivityLog.this);
            recyclerView.setAdapter(adapter);
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            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()) {
                            listItems.add(new ListItem(entry.getKey(), entry.getValue(),usersnapshot.child("Time").getValue().toString()
                            ));
                        }
                    }
                    recyclerView.setAdapter(new MyAdapter(getApplicationContext(),listItems));
                    adapter = new MyAdapter(listItems, ActivityLog.this);
                    recyclerView.setAdapter(adapter);
                }


                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    swipe.setRefreshing(false);
                }
            },2000);
            listItems.clear();
        }
    });
}

}

这是我的适配器

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;
}

public MyAdapter(Context applicationContext, List<ListItem> listItems) {
}

@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);

    String key = listItem.getHead();
        if(key.equals("LockTime"))
        {
            holder.textViewHead.setText(listItem.getTime());
            holder.textViewDesc.setText(listItem.getDesc());
            holder.layout.setBackground(ContextCompat.getDrawable(context, R.drawable.red_gradient));
            holder.image1.setImageResource(R.drawable.lockwhite);
            holder.image2.setImageResource(R.drawable.lockwhite);
        }
        else if(key.equals("UnlockTime")){
            holder.textViewHead.setText(listItem.getTime());
            holder.textViewDesc.setText(listItem.getDesc());
            holder.layout.setBackground(ContextCompat.getDrawable(context, R.drawable.green_gradient));
            holder.image1.setImageResource(R.drawable.unlockwhite);
            holder.image2.setImageResource(R.drawable.unlockwhite);
        }
}

@Override
public int getItemCount() {
    return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView textViewHead;
    public TextView textViewDesc;
    public RelativeLayout layout;
    public ImageView image1;
    public ImageView image2;


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

        textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
        textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
        layout= (RelativeLayout) itemView.findViewById(R.id.relativelayout);
        image1 = itemView.findViewById(R.id.padlock1);
        image2 = itemView.findViewById(R.id.padlock2);
    }
}



}

非常感谢任何帮助!

这是我的默认线性布局

enter image description here

以下是我手机上的屏幕截图

enter image description here

0 个答案:

没有答案