我正在Android Studio上为Android开发社交/消息应用,我想知道如何用JSONArray填充ListView (好友列表)。
My Array(来自Parse currentUser文件:
"friends": ["friend1","friend2","friend3"]
我的活动代码:
package com.mycompany.myapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.parse.ParseUser;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;
public class friendsTab extends Fragment {
private ListView friendsLst;
ParseUser current = ParseUser.getCurrentUser();
JSONArray friends;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.friendsview,container,false);
friendsLst = (ListView)v.findViewById(R.id.friends_list_view);
friends = current.getJSONArray("friends");
return v;
}
}
谢谢!
答案 0 :(得分:0)
好的,我会帮你的。
在onCreate()中的Activity中实例化容器视图和组件。还要创建适配器类的对象,以便可以调用它的构造函数并使用它。
private ListView lvFriends;
private FriendAdapter friendAdapter; // your adapter that will handle the data
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friendsview); // <-- this is your container layout name?
// instantiate your adapter class
friendAdapter = new FriendAdapter();
// instantiate your listView
lvFriends = (ListView) findViewById(R.id.friends_list_view);
// set up your adapter
friendAdapter = new FriendAdapter(MainActivity, parseUser); // pass your parseUser object to your adapter (or whatever information you want)
lvFriends.setAdapter(friendAdapter); // set your adapter
}
现在在适配器类中执行类似以下的操作
public class FriendAdapter extends BaseAdapter {
private Context mContext;
private ParseUser mParseUser;
// constructor
public FriendAdapter(Context context, ParseUser parseUser) {
this.mContext = context;
this.mParseUser = parseUser;
}
@Override
public int getCount() {
// return the size of your friends from your parseUser object
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
// return the individual item from your parseUser object
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// you did not post your item view that goes into your container so I will
// just assume that it is just a textview
View view = convertView;
ViewHolder holder; // use view holder pattern
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_friend, parent, false);
holder = new ViewHolder();
holder.tvFriendName = (TextView) view.findViewById(R.id.tv_friend_name);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
return voew;
}
private static class ViewHolder {
private TextView tvFriendName;
}
}
希望这会有所帮助。干杯!