如何在Android Studio中从一个列表视图到另一个列表视图获取复选框选中的项目?

时间:2018-09-27 05:41:59

标签: listview

package com.example.nilesh.loginpa;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

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

public class Userslist extends ArrayAdapter <UserProfile> {
    private Activity context;
    private List<UserProfile> userlist;
    ArrayList<String> selected = new ArrayList<String>();
    public Userslist(Activity context,List<UserProfile> userlist){
        super(context,R.layout.list_layout,userlist);
        this.context =context;
        this.userlist = userlist;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();

        View listViewItem = inflater.inflate(R.layout.list_layout, null, true);

        TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
        TextView textViewemail = (TextView) listViewItem.findViewById(R.id.textViewemail);
        CheckBox chk = (CheckBox) listViewItem.findViewById(R.id.checked);


        UserProfile userProfile = userlist.get(position);

        textViewName.setText(userProfile.getName());
        textViewemail.setText(userProfile.getEmail());

        return listViewItem;


    }


}

这是我的阵列适配器类,我已将复选框添加到列表视图中的每个项目,如上面的代码所示。现在,我想做的是我要选择复选框,并希望在新活动的新列表视图上选择复选框。

package com.example.nilesh.loginpa;

import java.util.ArrayList;

public class UserProfile {
    public String name;
    public String email;
    public String age;
    public UserProfile(){

    }

    public UserProfile(String name, String email, String age) {
        this.name = name;
        this.email = email;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }



    @Override

    public String toString(){
        return this.name + ":" + this.age;
    }
}

这是我的从db getter和setter获取值的类。

package com.example.nilesh.loginpa;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
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 java.util.ArrayList;
import java.util.List;

public class Adminhome extends AppCompatActivity {
    FirebaseAuth firebaseauth;
    DatabaseReference mref;
    Button btn;
    Switch sw;
    ArrayList <String> selected = new ArrayList<>();
    //private String uname;

    //private ArrayList<String> ulist = new ArrayList<>();

    List<UserProfile> userlist;
    ListView allusers;
    Switch switch1;


    //private ArrayAdapter<String> arrayAdapter;

    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adminhome);


        //firebaseauth = FirebaseAuth.getInstance();
        mref = FirebaseDatabase.getInstance().getReference("users");
        btn = (Button) findViewById(R.id.btnsub);
        sw = (Switch) findViewById(R.layout.list_layout);
        allusers = (ListView) findViewById(R.id.lvusers);
        userlist = new ArrayList<>();
        //arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ulist);


        }

    @Override
    protected void onStart() {
        super.onStart();

        mref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                userlist.clear();
                for (DataSnapshot userprofileSnapshot : dataSnapshot.getChildren()){
                    UserProfile userProfile = userprofileSnapshot.getValue(UserProfile.class);

                    userlist.add(userProfile);

                }

                Userslist adapter = new Userslist(Adminhome.this,userlist);

                allusers.setAdapter(adapter);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

}

在此类中,我从db firebase获取db项的快照。我是否必须做另一个listview才能在新的listview中获得所有复选框的检查项目,或者我必须做些什么,请帮助我我被困在这里,而且我是Android Studio的新手。预先谢谢你。

0 个答案:

没有答案