Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 不显示listview项的片段_Android_Mysql_Android Fragments_Android Arrayadapter - Fatal编程技术网

Android 不显示listview项的片段

Android 不显示listview项的片段,android,mysql,android-fragments,android-arrayadapter,Android,Mysql,Android Fragments,Android Arrayadapter,大家好,我有这个片段创建方法 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); user_id = sp.getString("user_id", "anon"); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mPar

大家好,我有这个片段创建方法

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    user_id = sp.getString("user_id", "anon");

    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.fragment_friends_list, null, false);

    listview = (ListView)view.findViewById(R.id.friends_list);
    fadapter = new FriendsAdapter(getActivity(), R.layout.single_friend);
    listview.setAdapter(fadapter);
    listview.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    fadapter.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            listview.setSelection(fadapter.getCount() - 1);
        }
    });
我在日志中看到我的数据提供者正在创建listitems 这个类在我的fragment.java中

 class GetFriends extends AsyncTask<String, String, String> {

    protected String doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user_id", user_id));

        Log.w("params", params.toString());

        Log.d("request!", "starting");
        // getting product details by making HTTP request
        jsonArray = jsonParser.makeHttpRequest(
                EVENTS_URL, "POST", params);

        Log.w("jsonArray", jsonArray.toString());
        if (jsonArray != null) {
            try {
                for (int i = 0; i < jsonArray.length(); i++) {
                    Log.w("jsonArray", jsonArray.getJSONObject(i).toString());

                    //CREATE ITEM FOR LISTVIEW
                    fadapter.add(new FriendsProvider(
                            jsonArray.getJSONObject(i).getInt("id"),
                            jsonArray.getJSONObject(i).getString("username")));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
也许我正在考虑将适配器和提供程序复杂化。 但它是从应用程序的其他部分工作的

但也许我必须刷新片段的UI或者类似的东西

编辑1:

public class FriendsAdapter extends ArrayAdapter<FriendsProvider>{

private List<FriendsProvider> friends_list = new ArrayList<FriendsProvider>();
private TextView friends_item;
Context ctx;

public FriendsAdapter(Context context, int resource) {
    super(context, resource);
    ctx = context;
}

@Override
public void add(FriendsProvider object) {
    friends_list.add(object);
    super.add(object);
}
@Override
public int getCount() {
    return friends_list.size();
}

@Override
public FriendsProvider getItem(int position) {
    return friends_list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null)
    {
        LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflator.inflate(R.layout.single_friend,parent,false);
    }
    friends_item = (TextView)convertView.findViewById(R.id.singleFriend);

    FriendsProvider provider = getItem(position);

    friends_item.setText(provider.username );

    return convertView;
}
公共类FriendsAdapter扩展了ArrayAdapter{
private List friends_List=新建ArrayList();
私人文本查看好友项目;
上下文ctx;
公共FriendsAdapter(上下文,int资源){
超级(上下文、资源);
ctx=上下文;
}
@凌驾
公共无效添加(FriendsProvider对象){
好友列表。添加(对象);
super.add(object);
}
@凌驾
public int getCount(){
返回好友列表。大小();
}
@凌驾
公共友谊提供者获取项目(内部位置){
返回好友列表。获取(位置);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null)
{
LayoutInflater充气器=(LayoutInflater)ctx.getSystemService(Context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(右布局。单亲,父,假);
}
friends\u item=(TextView)convertView.findViewById(R.id.singleFriend);
FriendsProvider provider=getItem(位置);
friends\u item.setText(provider.username);
返回视图;
}

}

我认为问题在于,当您完成在适配器中添加项目时,您从未调用
适配器。notifyDataSetChanged()

请尝试在AsyncTask中添加onPostExecute()

 protected void onPostExecute() {
     adapter.notifyDataSetChanged();
 }
getActivity().runOnUiThread(newrunnable(){是答案

getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (jsonArray != null) {
                        try {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                Log.w("jsonArray", jsonArray.getJSONObject(i).toString());

                                //CREATE ITEM FOR LISTVIEW
                                fadapter.add(new FriendsProvider(
                                        jsonArray.getJSONObject(i).getInt("id"),
                                        jsonArray.getJSONObject(i).getString("username")));
                                fadapter.notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
getActivity().runOnUiThread(新的Runnable()){
@凌驾
公开募捐{
if(jsonArray!=null){
试一试{
for(int i=0;i
您使用的FrameLayout可能存在问题,根据FrameLayout可能会阻止某些视图显示,我不知道如何使用FrameLayout修复它,但当我用LinearLayout替换它时,它起到了作用

FrameLayout设计用于在屏幕上屏蔽一个区域以显示单个项目。通常,FrameLayout应用于保存单个子视图,因为很难以可扩展到不同屏幕大小的方式组织子视图,而不会使子视图相互重叠。但是,您可以将多个子视图添加到F使用android:layout\u gravity属性,通过为每个子级指定重力,来控制它们在FrameLayout中的位置


@Konter我需要您复制并粘贴适配器的所有代码。如果不是,则非常复杂。该片段不会进入onPostExecute()。我会将适配器复制到第一个帖子中
public class FriendsAdapter extends ArrayAdapter<FriendsProvider>{

private List<FriendsProvider> friends_list = new ArrayList<FriendsProvider>();
private TextView friends_item;
Context ctx;

public FriendsAdapter(Context context, int resource) {
    super(context, resource);
    ctx = context;
}

@Override
public void add(FriendsProvider object) {
    friends_list.add(object);
    super.add(object);
}
@Override
public int getCount() {
    return friends_list.size();
}

@Override
public FriendsProvider getItem(int position) {
    return friends_list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null)
    {
        LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflator.inflate(R.layout.single_friend,parent,false);
    }
    friends_item = (TextView)convertView.findViewById(R.id.singleFriend);

    FriendsProvider provider = getItem(position);

    friends_item.setText(provider.username );

    return convertView;
}
 protected void onPostExecute() {
     adapter.notifyDataSetChanged();
 }
getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (jsonArray != null) {
                        try {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                Log.w("jsonArray", jsonArray.getJSONObject(i).toString());

                                //CREATE ITEM FOR LISTVIEW
                                fadapter.add(new FriendsProvider(
                                        jsonArray.getJSONObject(i).getInt("id"),
                                        jsonArray.getJSONObject(i).getString("username")));
                                fadapter.notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });