无法将列表添加到自定义列表视图。我在使用String []时遇到问题,我正试图从Firebase数据库中获取用户并将其存储到我的
private ArrayList<UserInfo> userInfos;
userInfos=new ArrayList<>();
但是当运行它进行仿真时,我的列表视图显示为空白,我试图检查List<String> friends = new ArrayList<>();
并且我的用户可用。有人可以帮助我吗?
我的MainActivityCustonlistViewnew.java
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
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.Arrays;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
public class MainActivityCustonlistViewnew extends AppCompatActivity {
private ArrayList<UserInfo> userInfos;
private CustomListAdapter customListAdapter;
private ListView customListView;
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
private DatabaseReference databaseReference = firebaseDatabase.getReference();
private DatabaseReference UserData = databaseReference.child("Users");
private List<String> friends = new ArrayList<>();
private String[] names ;
private String[] professions={
"Rank 1",
"Rank 2",
"Rank 3"
};
private int[] photos={
R.drawable.sample_5,
R.drawable.sample_1,
R.drawable.sample_6
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maincustomlistview);
setSupportActionBar((Toolbar)findViewById(R.id.toolbar));
// ArrayList arrayList = new ArrayList(friends);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String friend = ds.getKey();
friends.add(friend);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
UserData.addListenerForSingleValueEvent(eventListener);
customListView=(ListView)findViewById(R.id.custom_list_view);
userInfos=new ArrayList<>();
Arrays.sort(names, String.CASE_INSENSITIVE_ORDER);
customListAdapter=new CustomListAdapter(userInfos,MainActivityCustonlistViewnew.this);
customListView.setAdapter(customListAdapter);
getDatas();
customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivityCustonlistViewnew.this, "Name : " + names[i] + "\n Profession : " + professions[i], Toast.LENGTH_SHORT).show();
}
});
//sample-----
}
// getting all the datas
private void getDatas(){
for(int count=0;count<names.length;count++){
userInfos.add(new UserInfo(names[count],professions[count],photos[count]));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_option,menu);
MenuItem menuItem=menu.findItem(R.id.search);
SearchView searchView=(SearchView)menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
newText=newText.toString();
ArrayList<UserInfo> newUserInfos=new ArrayList<>();
for(UserInfo userInfo:userInfos){
String name=userInfo.getName().toLowerCase();
String profession=userInfo.getProfession().toLowerCase();
if(name.contains(newText) || profession.contains(newText)){
newUserInfos.add(userInfo);
}
}
customListAdapter.filterResult(newUserInfos);
customListAdapter.notifyDataSetChanged();
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
我的Userinfo.java
public class UserInfo {
private String name,profession;
private int photo;
public UserInfo(){}
public UserInfo(String name, String profession, int photo) {
this.name = name;
this.profession = profession;
this.photo = photo;
}
public String getName() {
return name;
}
public String getProfession() {
return profession;
}
public int getPhoto() {
return photo;
}
}
我的customlistAdapter
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class CustomListAdapter extends BaseAdapter implements View.OnClickListener{
private ArrayList<UserInfo> userInfos;
private Context context;
public CustomListAdapter(ArrayList<UserInfo> userInfos, Context context) {
this.userInfos = userInfos;
this.context = context;
}
@Override
public int getCount() {
return userInfos.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=layoutInflater.inflate(R.layout.custom_list_view_layout,null);
ImageView photo,option;
if(view==null){
photo=new ImageView(context);
}
UserInfo userInfo=userInfos.get(i);
photo=(ImageView)view.findViewById(R.id.photo);
option=(ImageView)view.findViewById(R.id.option);
TextView name=(TextView)view.findViewById(R.id.name);
TextView profession=(TextView)view.findViewById(R.id.profession);
photo.setImageResource(userInfo.getPhoto());
name.setText(userInfo.getName());
profession.setText(userInfo.getProfession());
option.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.option:
showPopupMenu(view);
break;
}
}
// getting the popup menu
private void showPopupMenu(View view){
PopupMenu popupMenu=new PopupMenu(context,view);
popupMenu.getMenuInflater().inflate(R.menu.option_menu,popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.edit:
Toast.makeText(context, "Edit !", Toast.LENGTH_SHORT).show();
return true;
case R.id.remove:
Toast.makeText(context, "Remove !", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
});
}
//file search result
public void filterResult(ArrayList<UserInfo> newUserInfos){
userInfos=new ArrayList<>();
userInfos.addAll(newUserInfos);
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
customListAdapter=new CustomListAdapter(userInfos,MainActivityCustonlistViewnew.this);
customListView.setAdapter(customListAdapter);
getDatas();
您在这里做错了。您正在调用customListAdapter并将此处的cusrom data(userInfos)设置为空。因为getDatas()方法在customListAdapter之后调用。因此,列表将为空。只需交换正电子即可。
getDatas();
customListAdapter=new CustomListAdapter(userInfos,MainActivityCustonlistViewnew.this);
customListView.setAdapter(customListAdapter);
希望有帮助。
谢谢 索拉卜 祝您编码愉快!
答案 1 :(得分:0)
您正在从getDatas()
添加数据,但是您的适配器已经被初始化为空数据。因此,有两种方法:
1:在getDatas()
调用后初始化并设置适配器
getDatas();
customListAdapter=new CustomListAdapter(userInfos,MainActivityCustonlistViewnew.this);
customListView.setAdapter(customListAdapter);
2:使用notifyDataSetChanged()
方法向适配器通知数据集更改
customListView.setAdapter(customListAdapter);
getDatas();
customListAdapter.notifyDataSetChanged();