我的ListView
未显示数据,我使用适配器类将数据设置为ListView
,但未显示。
MyAdapter类:
public class MyAdapter extends ArrayAdapter {
private Context context;
private LayoutInflater inflater;
private List<HashMap<String, String>> aList=new ArrayList<HashMap<String,String>>();
int[] to;
public MyAdapter(Context context, List<HashMap<String, String>> aList) {
super(context, R.layout.mainlistviewchild, aList);
this.context = context;
this.aList = aList;
inflater = LayoutInflater.from(context);
}
HashMap<String, String> m;
String[] aa;
String c;
List<String> myString = new ArrayList<String>();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = inflater.inflate(R.layout.mainlistviewchild, parent);
}
TextView title1=(TextView)convertView.findViewById(R.id.maintitle);
TextView description1=(TextView)convertView.findViewById(R.id.maindesc);
TextView date=(TextView)convertView.findViewById(R.id.mainby);
ImageView image1=(ImageView)convertView.findViewById(R.id.mainimage);
TextView locstate=(TextView)convertView.findViewById(R.id.maindate);
title1.setText(aList.get(position).get("Name"));
description1.setText(aList.get(position).get("Email"));
locstate.setText(aList.get(position).get("State"));
date.setText(aList.get(position).get("Country"));
Glide.with(context).load(aList.get(position).get("Image")).into(image1);
return convertView;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public MainActivity() {
// Required empty public constructor
}
DatabaseReference ref;
DatabaseReference postRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
ref = FirebaseDatabase.getInstance().getReference();
String stuff=getIntent().getExtras().getString("stuff");
ListView listView=(ListView)findViewById(R.id.listView);
final List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
final MyAdapter adapter = new MyAdapter(getApplicationContext(),aList);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
postRef=ref.child("Users");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
aList.clear();
for (DataSnapshot child : dataSnapshot.getChildren()) {
for (DataSnapshot single : child.getChildren()) {
Map<String, Object> map = (Map<String, Object>) single.getValue();
String name = (String) map.get("Name");
String image = (String) map.get("Profileimage");
String email = (String) map.get("Email");
String address=(String) map.get("Address");
String state=(String) map.get("State");
String country=(String) map.get("Country");
String city=(String) map.get("City");
String knownplace=(String) map.get("Knownplace");
String postalcode=(String) map.get("Postalcode");
if (name!=null && image!=null && email!=null && address!=null && country!=null && state!=null && city!=null && knownplace!=null && postalcode!=null) {
String[] name1={name};
String[] email1={email};
String[] address1={address};
String[] image1={image};
String[] state1={state};
String[] country1={country};
String[] city1={city};
String[] knownplace1={knownplace};
String[] postalcode1={postalcode};
HashMap<String,String> data=new HashMap<String, String>();
for (int i=0; i<name1.length;i++) {
data.put("Name", name1[i]);
data.put("Email",email1[i]);
data.put("Address",address1[i]);
data.put("Image",image1[i]);
data.put("Country",country1[i]);
data.put("State",state1[i]);
data.put("City",city1[i]);
data.put("Knownolace",knownplace1[i]);
data.put("Postalcode",postalcode1[i]);
}
aList.add(data);
adapter.notifyDataSetChanged();
//progressBar.setVisibility(View.INVISIBLE);
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mainlistview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/mainimage"
android:src="@drawable/profile"
android:layout_below="@+id/relativeLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout">
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
android:textStyle="italic"
android:textSize="20sp"
android:id="@+id/maindesc"
android:layout_below="@+id/maintitle"
android:layout_toRightOf="@+id/mainimage"
android:layout_toEndOf="@+id/mainimage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textStyle="bold"
android:textSize="20sp"
android:layout_below="@+id/relativeLayout"
android:layout_toRightOf="@+id/mainimage"
android:layout_toEndOf="@+id/mainimage"
android:id="@+id/maintitle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:id="@+id/mainlocation"
android:layout_below="@+id/maindesc"
android:layout_toRightOf="@+id/mainimage"
android:layout_toEndOf="@+id/mainimage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Date"
android:id="@+id/maindate"
android:layout_above="@+id/mainby"
android:layout_toRightOf="@+id/mainimage"
android:layout_toEndOf="@+id/mainimage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="By"
android:id="@+id/mainby"
android:layout_alignBottom="@+id/mainimage"
android:layout_toRightOf="@+id/mainimage"
android:layout_toEndOf="@+id/mainimage" />
</RelativeLayout>
ListView
未显示任何数据
有人可以说这个问题吗?
答案 0 :(得分:0)
嘿@Shiv uday首先你需要把aList.add(数据);在for循环中
答案 1 :(得分:0)
您必须在适配器中覆盖getCount。
应该是
@Override
public int getCount() {
return aList.size();
}
和convertView应该是:
convertView = inflater.inflate(R.layout.mainlistviewchild, parent, false);