Java CustomAdapter不显示数据
我试图从解析中检索数据,因此我需要为listview创建一个自定义适配器,但它不起作用,我创建了一个自定义适配器,但listview没有显示,我无法找出问题所在。 我没有遇到错误或崩溃,因此我无法指出确切的问题 这就是我所做的: 适配器:Java CustomAdapter不显示数据,java,android,parsing,listview,custom-adapter,Java,Android,Parsing,Listview,Custom Adapter,我试图从解析中检索数据,因此我需要为listview创建一个自定义适配器,但它不起作用,我创建了一个自定义适配器,但listview没有显示,我无法找出问题所在。 我没有遇到错误或崩溃,因此我无法指出确切的问题 这就是我所做的: 适配器: public class MyListView extends BaseAdapter { private final ArrayList<HashMap<String, String>> contentList; p
public class MyListView extends BaseAdapter {
private final ArrayList<HashMap<String, String>> contentList;
private static LayoutInflater inflater = null;
public MyListView(Activity a, ArrayList<HashMap<String, String>> contentForList) {
this.contentList = contentForList;
inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return contentList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.mylist, null);
ImageView logo = (ImageView) vi.findViewById(R.id.icon); // title
TextView titleText = (TextView) vi.findViewById(R.id.title); // artist name
TextView subtitleText = (TextView) vi.findViewById(R.id.subtitle);
HashMap<String, String> info = new HashMap<String, String>();
info = contentList.get(position);
String title = (String) info.get("businessname");
String peopleinBusiness = (String) info.get("peopleinbusiness");
// Setting all values in listview
titleText.setText(title);
subtitleText.setText(peopleinBusiness);
// Bitmap bmp = (Bitmap) info.get("photo");
// logo.setImageBitmap(bmp);
return vi;
}
}
公共类MyListView扩展了BaseAdapter{
私有最终ArrayList内容列表;
专用静态充气机=空;
公共MyListView(活动a,ArrayList contentForList){
this.contentList=contentForList;
充气器=(LayoutInflater)a.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
public int getCount(){
返回contentList.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机充气(R.layout.mylist,空);
ImageView徽标=(ImageView)vi.findViewById(R.id.icon);//标题
TextView titleText=(TextView)vi.findViewById(R.id.title);//艺术家姓名
TextView subtitle text=(TextView)vi.findViewById(R.id.subtitle);
HashMap info=新的HashMap();
info=contentList.get(位置);
String title=(String)info.get(“businessname”);
String peopleinBusiness=(String)info.get(“peopleinBusiness”);
//在listview中设置所有值
titleText.setText(标题);
subtitleText.setText(peopleinBusiness);
//位图bmp=(位图)信息获取(“照片”);
//logo.setImageBitmap(bmp);
返回vi;
}
}
listView XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
主要活动:
ArrayList<HashMap<String,String>> feedData = new ArrayList<>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.orderByDescending("createdAt");
query.setLimit(20);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null){
for (ParseObject feed : objects){
HashMap<String, String> PlacesInfo = new HashMap<>();
PlacesInfo.put("username",feed.getString("username"));
PlacesInfo.put("worthcomming",feed.getString("worthcomming"));
PlacesInfo.put("businessname",feed.getString("BusinessName"));
PlacesInfo.put("peopleinbusiness",feed.getString("PeopleInBusiness"));
feedData.add(PlacesInfo);
}
MyListView listview = new MyListView(FeedActivity.this,feedData);
feedListView.setAdapter(listview);
listview.notifyDataSetChanged();
}else{
Log.e("findinBackground","Failed");
e.printStackTrace();
}
}
});
ArrayList feedData=new ArrayList();
ParseQuery=ParseQuery.getQuery(“用户”);
query.orderByDescending(“createdAt”);
查询.设定限额(20);
findInBackground(新的FindCallback(){
@凌驾
公共void done(列出对象,parsee异常){
如果(e==null){
for(ParseObject提要:对象){
HashMap PlacesInfo=新的HashMap();
PlacesInfo.put(“用户名”),feed.getString(“用户名”);
PlacesInfo.put(“worthcoming”,feed.getString(“worthcoming”);
PlacesInfo.put(“businessname”,feed.getString(“businessname”);
PlacesInfo.put(“peopleinbusiness”,feed.getString(“peopleinbusiness”);
feedData.add(PlacesInfo);
}
MyListView listview=新的MyListView(FeedActivity.this,feedData);
setAdapter(listview);
notifyDataSetChanged();
}否则{
Log.e(“findinBackground”,“Failed”);
e、 printStackTrace();
}
}
});
谢谢 我真不敢相信我花了这么多时间才发现我错过了一个“\ux” 我改变了这一点:
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
ParseQuery query=ParseQuery.getQuery(“用户”);
为此:
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
ParseQuery query=ParseQuery.getQuery(“_User”);
谢谢大家 如果ListView没有显示任何内容,首先要检查数据列表的size(),因此在query done()中添加Log来检查objects.size()和feedData.size()。若数据列表不是空的,那个么检查是否调用了适配器,所以在getView()中添加日志。若调用了适配器,那个么问题可能是活动的布局或ListView。另一方面,无需制作HashMaps,直接将列表传递到适配器()。嘿@i_A_mok谢谢您的回复!我只是记录了您指定的数据,其中没有数据。当我将它们记录到for循环中时,它不会记录任何内容,但如果我在它显示0之后记录。我不明白为什么,因为解析中存在“User”文件夹。(解析照片-)我对解析平台没有太多经验,不知道问题出在哪里。可能是try ParseUser,例如,
ParseQuery userQuery=ParseUser.getQuery();userQuery.findInBackground(新的FindCallback(){void done(列表结果,ParseException e){……}
而不是ParseObject。