Recyclerview中的计时器不会更改,因此尝试将列表更改为Hashmap

时间:2019-12-11 06:04:32

标签: android arraylist android-recyclerview hashmap chronometer

我已经制作了有关输入的会员列表的示例应用程序。制作应用并运行很好。但是,当我从列表中删除成员时,Recyclerview中的Chronometer不会更改,并保持删除成员的时间。另外,所有清单的计时器都从上一项的经过时间开始运行。

认为这是数组列表的限制,所以我也尝试将arraylist更改为Hashmap和list的项目。但是将某些代码更改为Hashmap会导致Nullpoint异常,并且我的编码现在停止了。

如何在清单项目中使用Chronometer制作Recyclerview应用程序?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/located member Live"
        android:textColor="#000000"
        android:textSize="40sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/Simplelist"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title"/>

    <ToggleButton
        android:id="@+id/nfcbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="24dp"
        android:textSize="20sp"
        android:textOn="@string/nfcoff"
        android:textOff="@string/nfcon"
        app:layout_constraintStart_toStartOf="@id/Simplelist"
        app:layout_constraintTop_toBottomOf="@+id/Simplelist" />

    <Button
        android:id="@+id/locateall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:text="@string/locate all"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/Simplelist"
        app:layout_constraintTop_toBottomOf="@+id/Simplelist" />

    <Button
        android:id="@+id/addmemb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add member"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="@id/locateall"
        app:layout_constraintStart_toStartOf="@id/nfcbutton"
        app:layout_constraintTop_toBottomOf="@id/nfcbutton" />

    <EditText
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:hint="member number"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="@id/addmemb"
        app:layout_constraintTop_toBottomOf="@id/addmemb"
        android:id="@+id/edit"/>

</androidx.constraintlayout.widget.ConstraintLayout>

itemsimple.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="90dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/specPic"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/p11"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_marginTop="10sp">

            <TextView
                android:id="@+id/specRank"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:text="A"/>

            <TextView
                android:id="@+id/specName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:layout_marginLeft="5sp"
                android:text="Name"/>

            <Chronometer
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="30sp"
                android:layout_marginLeft="20sp"
                android:id="@+id/timer"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

listadapter.java

package com.example.myapplication1;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class listadapter extends RecyclerView.Adapter<listadapter.CustomViewHolder> {

    private ArrayList<listdata> arrayList ;

    public listadapter(ArrayList<listdata> arrayList) {
        this.arrayList = arrayList;

    }

    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemsimple,parent,false);
        CustomViewHolder holder = new CustomViewHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull final listadapter.CustomViewHolder holder, int position) {

        holder.specPic.setImageResource(arrayList.get(position).getPic());
        holder.specRank.setText(arrayList.get(position).getRank());
        holder.specName.setText(arrayList.get(position).getName());
        holder.timer.start();


        //Onclick Listener
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String curName = holder.specName.getText().toString();
                Toast.makeText(v.getContext(), curName, Toast.LENGTH_SHORT).show();
            }
        });
    }



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

    public class CustomViewHolder extends RecyclerView.ViewHolder {

        protected ImageView specPic ;
        protected TextView specRank ;
        protected TextView specName ;
        public Chronometer timer;


        public CustomViewHolder(@NonNull View itemView) {
            super(itemView);
            this.specPic = (ImageView) itemView.findViewById(R.id.specPic) ;
            this.specRank = (TextView) itemView.findViewById(R.id.specRank) ;
            this.specName = (TextView) itemView.findViewById(R.id.specName) ;
            this.timer=(Chronometer) itemView.findViewById(R.id.timer);
        }
    }
}

MainActivity.java

package com.example.myapplication1;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Iterator;

public class MainActivity extends AppCompatActivity {

    private ArrayList arrayList;
    private listadapter listadapter;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.Simplelist);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        arrayList = new ArrayList<>();
        listadapter = new listadapter(arrayList);
        recyclerView.setAdapter(listadapter);
        final EditText edit;
        edit = (EditText) findViewById(R.id.edit);



        Button locateall = (Button) findViewById(R.id.locateall);
        resetbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arrayList.clear();
                listdata set1 = new listdata(R.drawable.p41, "D rank", "Hodol", "set1");
                listdata set2 = new listdata(R.drawable.p31, "C rank", "kheoldol", "set2");
                listdata set3 = new listdata(R.drawable.p51, "B rank", "nwti", "set3");
                listdata set4 = new listdata(R.drawable.p11, "A rank", "uwngi", "set4");
                listdata set5 = new listdata(R.drawable.p21, "A rank", "youngi", "set5");
                arrayList.add(set1);
                arrayList.add(set2);
                arrayList.add(set3);
                arrayList.add(set4);
                arrayList.add(set5);
                listadapter.notifyDataSetChanged();
            }
        });
        Button addmemb = (Button) findViewById(R.id.addmemb);
        tolist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = edit.getText().toString();
                search(text);
                edit.setText("");
                listadapter.notifyDataSetChanged();
            }
        });
    }

    public void search(String text) {
        Iterator<listdata> it = arrayList.iterator();
        while (it.hasNext()) {
            String value = it.next().getnum();
            if (value.equals(text)) {
                it.remove();
            return;
            }
        }
                if (text.equals("set1")) {
                    listdata set1 = new listdata(R.drawable.p41, "D rank", "Hodol", "set1");
                    arrayList.add(set1);
                } else if (text.equals("set2")) {
                    listdata set2 = new listdata(R.drawable.p31, "C rank", "kheoldol", "set2");
                    arrayList.add(set2);
                } else if (text.equals("set3")) {
                    listdata set3 = new listdata(R.drawable.p51, "B rank", "nwti", "set3");
                    arrayList.add(set3);
                } else if (text.equals("set4")) {
                    listdata set4 = new listdata(R.drawable.p11, "A rank", "uwngi", "set4");
                    arrayList.add(set4);
                } else if (text.equals("set5")) {
                    listdata set5 = new listdata(R.drawable.p21, "A rank", "youngi", "set5");
                    arrayList.add(set5);
                } else {
                    Toast.makeText(this.getApplicationContext(), "It is not enrolled member!", Toast.LENGTH_SHORT).show();
                }
    }

}

0 个答案:

没有答案