【发布时间】:2021-07-28 15:53:39
【问题描述】:
我正在尝试从解析中检索数据,因此我需要为列表视图制作一个自定义适配器,但它不起作用,我创建了一个自定义适配器,但列表视图没有显示,我不知道是什么问题. 我没有收到任何错误或崩溃,所以我无法指出确切的问题。
这就是我所做的:
适配器:
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;
}
}
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();
}
}
});
谢谢!!
【问题讨论】:
-
如果ListView什么都没有显示,首先是检查数据列表的size(),所以添加Log检查objects.size()和feedData .size() 在查询 done() 中。如果数据列表不为空,则检查适配器是否被调用,所以在 getView() 中添加 Log。如果调用了适配器,那么问题可能是 Activity 或 ListView 的布局。另一方面,不需要制作HashMaps,直接将List
传递给适配器(stackoverflow.com/questions/42050789/…)。 -
嘿@i_A_mok 谢谢你的回复!我刚刚记录了您指定的数据,但其中没有数据。当我将它们登录到 for 循环时,它不会记录任何内容,但如果我在它显示为 0 后记录它。我不明白为什么,因为存在“用户”文件夹在解析中。 (解析照片-ibb.co/vDVrzfm)
-
我对 Parse Platform 没有太多经验,不知道问题出在哪里。可以尝试 ParseUser,例如
ParseQuery<ParseUser> userQuery = ParseUser.getQuery(); userQuery.findInBackground(new FindCallback<ParseUser>() { void done(List<ParseUser> results, ParseException e) {......}而不是 ParseObject.
标签: java android parsing listview custom-adapter