我想在下一个活动中关闭所有用户,但是我不能这样做。 listview
中的每个项目都有开关,但是我无法存储或获取每个项目的状态。
我想将开关添加到所有项目,并且要保存开关的所有状态。我想访问新活动中所有处于关闭状态的项目。
这是adminhome.java文件,它显示此活动页面上的所有列表视图项:
package com.example.nilesh.loginpa;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
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;
//private String uname;
//private ArrayList<String> ulist = new ArrayList<>();
List<UserProfile> userlist;
ListView allusers;
Switch switch1;
//private ArrayAdapter<String> arrayAdapter;
@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);
allusers = (ListView) findViewById(R.id.lvusers);
userlist = new ArrayList<>();
//arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ulist);
//allusers.setAdapter(arrayAdapter);
}
@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) {
}
});
}
}
这是UserList.java文件,用于在列表视图中保存项目:
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.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import java.util.List;
public class Userslist extends ArrayAdapter <UserProfile> {
private Activity context;
private List<UserProfile> userlist;
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);
Switch switch1 = (Switch) listViewItem.findViewById(R.id.switch1);
UserProfile userProfile = userlist.get(position);
textViewName.setText(userProfile.getName());
textViewemail.setText(userProfile.getEmail());
return listViewItem;
}
}
这是UserProfile.java代码,用于从Firebase访问所有用户详细信息:
package com.example.nilesh.loginpa;
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;
}
}
答案 0 :(得分:0)
请注意这个例子。您需要进行XML布局,使列表项具有所需的外观(在此示例中,列表项由一个textview和一个togglebutton组成):
private class ShopItemListAdapter extends BaseAdapter {
private List<ShopItem> shopItems;
private ShopItemListAdapter(List<ShopItem> shopItems) {
this.shopItems = shopItems;
}
@Override
public int getCount() {
return shopItems.size();
}
@Override
public Object getItem(int position) {
return shopItems.get(position);
}
@Override
public long getItemId(int position) {
return shopItems.get(position).getId();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ShopItem shopItem = shopItems.get(position);
if (convertView == null) {
LayoutInflater layoutInflater = getLayoutInflater();
convertView = layoutInflater.inflate(R.layout.shopitem, null);
TextView textView = convertView.findViewById(R.id.shopItemText);
ToggleButton toggleButton = convertView.findViewById(R.id.shopItemBought);
ViewHolder viewHolder = new ViewHolder(toggleButton, textView);
convertView.setTag(viewHolder);
}
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
// click listener must be set always no matter of convertView is or is not null
// otherwise recycle view behaves strangely to checked states of toggle buttons
viewHolder.shopItemBought.setOnClickListener(new ToggleBoughtListener(shopItem));
viewHolder.shopItemBought.setChecked(shopItem.isBought());
viewHolder.shopItemName.setText(shopItem.getName());
return convertView;
}
private class ViewHolder {
ToggleButton shopItemBought;
TextView shopItemName;
ViewHolder(ToggleButton shopItemBought, TextView shopItemName) {
this.shopItemBought = shopItemBought;
this.shopItemName = shopItemName;
}
}
private class ToggleBoughtListener implements View.OnClickListener {
final ShopItem shopItem;
private ToggleBoughtListener(ShopItem shopItem) {
this.shopItem = shopItem;
}
@Override
public void onClick(View v) {
shopItem.setBought(!shopItem.isBought());
repository.update(shopItem);
}
}
}