Android 应用程序中的适配器问题

发布于 2024-11-25 08:30:02 字数 3488 浏览 7 评论 0原文

我在 android 中有一个应用程序,其中我从 facebook 返回一些数据(一些名称和 ID),在返回这些数据后,我使用适配器在列表视图中设置所有这些名称。 所有这些工作我都在 onCreate() 中完成。第一次我使用方法 getData() 从 facebook 返回一些名称,之后我调用适配器。

问题是 getData() 在字符串的 array 中返回将使用适配器设置的名称。现在,调用的适配器对此数组进行一些处理。

我如何从网络返回这些名称这部分工作缓慢并且数组没有及时填充,因此当适配器被调用时数组仍然是空的并且我强制关闭。

这就是我的做法:

private String[] mStrings;
private String[] fName;
public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
       setContentView(R.layout.invitefriends);

        ....................
   /*In here I request for friends from facebook which ae set up in an array*/
     mAsyncRunner.request("me/friends", new SampleRequestListener());

  /*here is the adapter that processes the arrays returned in SampleRequestListener*/

    adapter=new LazyAdapter1(this, mStrings,fName);
     list.setAdapter(adapter);

  }

/这是从 facebook 返回某些数组中的名称的 SampleRequestListener/

public class SampleRequestListener extends BaseRequestListener{
    int i;
    public void onComplete(final String response, final Object state){
        friends = new ArrayList<Friends>();
        try{
            Log.d("facebook-example","Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            JSONArray array = json.optJSONArray("data");


            if(array!=null){
                for(int i=0; i<array.length(); i++)
                {
                    String name=array.getJSONObject(i).getString("name");
                    String id= array.getJSONObject(i).getString("id");
                    Friends f=new Friends(name,id);
                    friends.add(f);
                    Log.d(name,id);
                }
                   mStrings = new String[friends.size()];
                    for(int i=0; i< mStrings.length; i++){
                        mStrings[i] = "http://graph.facebook.com/"+friends.get(i).getId()+"/picture?type=small";
                    }
                    fName = new String[friends.size()];
                    for(int i=0; i<friends.size(); i++)
                    {
                        fName[i]=friends.get(i).getName();
                    }

            }
        }
        catch(JSONException e)
        {
            //do nothing
        }
        catch(FacebookError e)
        {
            e.getMessage();
        }
    }
}

最后这是适配器的主体:

public class LazyAdapter1 extends BaseAdapter {

private Activity activity;
private String[] data;
private String[] nume;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter1(Activity a, String[] d, String[] f) {
    activity = a;
    data=d;
    nume=f;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView text;
    public ImageView image;
}

public View getView(int position, View convertView, ViewGroup parent) {
    //........
}

}

问题是 < code>mStrings[] 和 fName[] 后台线程没有及时返回,因此当我尝试对它们使用适配器时,它们仍然是空的。

那么有人可以告诉我应该如何继续让这件事正常工作吗?谢谢

I have an app in android in which I return some data from facebook(some names and id's) and right after I return this data I use an adapter to set all this names in a list view.
All this work I do it in onCreate().First time I use a method getData() to return some names from facebook and after that I call for the adapter.

The problem is that getData() returns in an array of Strings the names that will be setup up with the adapter.Now,the adapter called does some processing on this array.

How I return these names from web this part works slowly and the array is not filled in time so when the adapter gets called the array is still empty and I get force close.

this is how I do it:

private String[] mStrings;
private String[] fName;
public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
       setContentView(R.layout.invitefriends);

        ....................
   /*In here I request for friends from facebook which ae set up in an array*/
     mAsyncRunner.request("me/friends", new SampleRequestListener());

  /*here is the adapter that processes the arrays returned in SampleRequestListener*/

    adapter=new LazyAdapter1(this, mStrings,fName);
     list.setAdapter(adapter);

  }

/Here is the SampleRequestListener that does return from facebook the names in some arrays/

public class SampleRequestListener extends BaseRequestListener{
    int i;
    public void onComplete(final String response, final Object state){
        friends = new ArrayList<Friends>();
        try{
            Log.d("facebook-example","Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            JSONArray array = json.optJSONArray("data");


            if(array!=null){
                for(int i=0; i<array.length(); i++)
                {
                    String name=array.getJSONObject(i).getString("name");
                    String id= array.getJSONObject(i).getString("id");
                    Friends f=new Friends(name,id);
                    friends.add(f);
                    Log.d(name,id);
                }
                   mStrings = new String[friends.size()];
                    for(int i=0; i< mStrings.length; i++){
                        mStrings[i] = "http://graph.facebook.com/"+friends.get(i).getId()+"/picture?type=small";
                    }
                    fName = new String[friends.size()];
                    for(int i=0; i<friends.size(); i++)
                    {
                        fName[i]=friends.get(i).getName();
                    }

            }
        }
        catch(JSONException e)
        {
            //do nothing
        }
        catch(FacebookError e)
        {
            e.getMessage();
        }
    }
}

And finally here is the adapter's body:

public class LazyAdapter1 extends BaseAdapter {

private Activity activity;
private String[] data;
private String[] nume;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter1(Activity a, String[] d, String[] f) {
    activity = a;
    data=d;
    nume=f;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView text;
    public ImageView image;
}

public View getView(int position, View convertView, ViewGroup parent) {
    //........
}

}

The problem is that mStrings[] and fName[] are not returned in time by the background thread so they are still empty when I try to act with the adapter on them.

So could someone tell how should I proceed for this thing to work right??Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2024-12-02 08:30:02

使用 AsyncTask 类并使用您的自定义类进行扩展。

这对于在后台进程中获取数据很有好处。

当获取数据然后将其设置到适配器中时,

请检查此链接

http://www.vogella .de/articles/AndroidPerformance/article.html

http://developer.android.com/reference/android/os/AsyncTask.html

http://www.xoriant.com/blog/mobile-application-development/android-async-task.html

use the AsyncTask class and extend with your custom class.

this was good for getting the data in background process.

and when the data are fetched then set into the adapter

check this links

http://www.vogella.de/articles/AndroidPerformance/article.html

http://developer.android.com/reference/android/os/AsyncTask.html

http://www.xoriant.com/blog/mobile-application-development/android-async-task.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文