开发者

problem with an adapter in android app

开发者 https://www.devze.com 2023-03-21 09:02 出处:网络
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.

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 开发者_JS百科 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号