Show Data In Listview With Asynctask
I success show my data from web service JSON in listview, but I want to add Asyntask. Where I can put code Asyntask in my code. This my code to show data in list view public class
Solution 1:
Full ASyncTask Eclipse Project,
and here's some code that I think, when mixed with the above sample, will get you the result with the list that you desire (you'll have to adapt it to your needs a bit, though (pay attention to the list stuff, even though this is from a custom Dialog:
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builderbuilder=newAlertDialog.Builder(getActivity());
builder.setTitle("Kies Facebook-account");
builder.setNegativeButton("Cancel", this);
LayoutInflaterinflater= (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewdialogLayout= inflater.inflate(R.layout.dialog, null);
builder.setView(dialogLayout);
final String[] items = {"Red", "Green", "Blue" };
builder.setAdapter(newArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items),
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
Log.v("touched: ", items[which].toString());
}}
);
return builder.create();
}
Solution 2:
This is my code please try this one,
MAinActivity.java
publicclassMyActivityextendsActivity {
privateListView contests_listView;
privateProgressBar pgb;
ActivitiesBean bean;
ArrayList<Object> listActivities;
ActivityAdapter adapter;
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
contests_listView = (ListView) findViewById(R.id.activity_listView);
pgb = (ProgressBar) findViewById(R.id.contests_progressBar);
listActivities = newArrayList<Object>();
newFetchActivitesTask().execute();
}
publicclassFetchActivitesTaskextendsAsyncTask<Void, Void, Void> {
int i =0;
@OverrideprotectedvoidonPreExecute() {
// TODO Auto-generated method stubsuper.onPreExecute();
pgb.setVisibility(View.VISIBLE);
}
@OverrideprotectedVoiddoInBackground(Void... params) {
// TODO Auto-generated method stubString url = "Your URL Here";
String strResponse = util.makeWebCall(url);
try {
JSONObject objResponse = newJSONObject(strResponse);
JSONArray jsonnodes = objResponse.getJSONArray(nodes);
for (i = 0; i < jsonnodes.length(); i++)
{
String str = Integer.toString(i);
Log.i("Value of i",str);
JSONObject jsonnode = jsonnodes.getJSONObject(i);
JSONObject jsonnodevalue = jsonnode.getJSONObject(node);
bean = newActivitiesBean();
bean.title = jsonnodevalue.getString(title);
bean.image = jsonnodevalue.getString(field_activity_image_fid);
listActivities.add(bean);
}
}
catch (JSONException e) {
e.printStackTrace();
}
returnnull;
}
@OverridepublicvoidonPostExecute(Void result) {
// TODO Auto-generated method stubsuper.onPostExecute(result);
pgb.setVisibility(View.GONE);
displayAdapter();
}
}
publicvoiddisplayAdapter()
{
adapter = newActivityAdapter(this, listActivities);
contests_listView.setAdapter(adapter);
contests_listView.setOnItemClickListener(newOnItemClickListener() {
//@OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// your onclick Activity
}
});
}
}
util.class
publicstatic String makeWebCall(String url) {
DefaultHttpClientclient=newDefaultHttpClient();
HttpGethttpRequest=newHttpGet(url);
// HttpPost post = new HttpPost(url);try {
HttpResponsehttpResponse= client.execute(httpRequest);
finalintstatusCode= httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
returnnull;
}
HttpEntityentity= httpResponse.getEntity();
InputStreaminstream=null;
if (entity != null) {
instream = entity.getContent();
}
return iStream_to_String(instream);
}
catch (IOException e) {
httpRequest.abort();
// Log.w(getClass().getSimpleName(), "Error for URL =>" + url, e);
}
returnnull;
}
publicstatic String iStream_to_String(InputStream is1) {
BufferedReaderrd=newBufferedReader(newInputStreamReader(is1), 4096);
String line;
StringBuildersb=newStringBuilder();
try {
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringcontentOfMyInputStream= sb.toString();
return contentOfMyInputStream;
}
}
ActivityBean.java
publicclassActivitiesBeanimplementsSerializable{
/**
*
*/privatestatic final long serialVersionUID = 1L;
publicString title;
publicString image;
publicStringgetTitle() {
return title;
}
publicvoidsetTitle(String title) {
this.title = title;
}
publicStringgetImage() {
return image;
}
publicvoidsetImage(String image) {
this.image = image;
}
Post a Comment for "Show Data In Listview With Asynctask"