How to Fetch Friends List of Twitter | Techbirds

Here I am going to showing how to fetch list of all friends and followers on Twitter on a button click.

Lets take a button ‘btnFetchFollowers’.
On click of button list of friends and followers are displayed . Code on button’s click :

btnFetchFollowers.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // Fetch List of Followers new LoadFollowers().execute(“”); try { Intent i = new Intent(MainActivity.this,DisplayFollowersList.class); startActivity(i); } catch (Exception ex) { ex.printStackTrace(); } }

});

Here we see that we called a AsyncTask ‘LoadFollowers’ that loads the list of friends and followers.
Implementation of ‘LoadFollowers’ is :

class LoadFollowers extends AsyncTask { ProgressDialog progressDialog; @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); try { progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setMessage(getResources().getString( R.string.loading)); progressDialog.setIndeterminate(true); progressDialog.setCancelable(false); progressDialog.show(); } catch (Exception e) { e.printStackTrace(); }

}

@Override protected Void doInBackground(String… params) { // TODO Auto-generated method stub

try {

Log.i(“act twitter………..”,
“ModifiedCustomTabBarActivity.class”);

ids = twitter.getFriendsIDs(-1);// ids

do {
for (long id_no : ids.getIDs()) {

ID = “followers ID #” + id; firstname = ID.split(“#”); first_Name = firstname[0];

id = firstname[1];

Log.i(“split………..”, first_Name + id);

name = twitter.showUser(id_no).getName(); arraylist.add(name); screenname = twitter.showUser(id_no).getScreenName();

imageUrl = twitter.showUser(id_no).getProfileImageURL();

imagelist.add(imageUrl); }

} while (ids.hasNext());

} catch (Exception ex) { ex.printStackTrace(); } return null;

}

@Override protected void onPostExecute(Void result) {

super.onPostExecute(result);

try { if (progressDialog.isShowing()) { progressDialog.cancel(); progressDialog = null; Intent i = new Intent(MainActivity.this, DisplayFollowersList.class); startActivity(i);

}

} catch (Exception e) {

} }

}

Till here we show that how we fetch the list. Now lets see how to implement the UI for it. For UI see step by step procedure :

1. Implementation of DisplayFollowersList class

public class DisplayFollowersList extends Activity { ListView list; ArrayList arraylist = MainActivity.arraylist; ArrayList imagelist = MainActivity.imagelist; ListView lv; Context context;

ArrayList prgmName;

@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState);

setContentView(R.layout.displaylist);

context = this; list = (ListView) findViewById(R.id.display_listview);

list.setAdapter(new CustomAdapter(DisplayFollowersList.this, arraylist, imagelist));

}
}

2. Implementation of CustomAdapter class

public class CustomAdapter extends BaseAdapter { ArrayList result; ArrayList imageId; Context context; private static LayoutInflater inflater = null;

AQuery aq;

public CustomAdapter(DisplayFollowersList displayList,  ArrayList NameList, ArrayList Images) { // TODO Auto-generated constructor stub result = NameList; context = displayList; imageId = Images; aq = new AQuery(displayList); inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override public int getCount() { // TODO Auto-generated method stub return result.size();

}

public class Holder { TextView tv; ImageView img;

}

@Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Holder holder = new Holder(); View rowView; rowView = inflater.inflate(R.layout.follower, null); holder.tv = (TextView) rowView.findViewById(R.id.textView1); holder.img = (ImageView) rowView.findViewById(R.id.imageView1); holder.tv.setText(result.get(position)); aq.id(holder.img.getId()).image(imageId.get(position)); rowView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(context, “You Clicked ” + result.get(position), Toast.LENGTH_LONG).show(); } }); return rowView;

}

@Override public long getItemId(int position) { // TODO Auto-generated method stub return position;

}

@Override public Object getItem(int position) { // TODO Auto-generated method stub return position; }

}

3. Implementation of layout displaylist.xml

4. Implementation of layout follower.xml

1,542 total views, 1 views today

Share this Onfacebook-9195631twitter-8401949linkedin-9340695google-6589130