using AsyncTask to display data in ListView - android

I need me a little help. I need to use asynctask to display data in ListView. But I don't know how becouse I'm new in Android programming ... thank you very much for any help.
public class Main extends ListActivity {
Button buttonbg;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://10.10.10.10/data.php");
try{
JSONArray ip = json.getJSONArray("ip");
for(int i=0;i<ip.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = ip.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("data1", e.getString("date"));
map.put("data2", "Location:" + e.getString("location") + " Status:" + e.getString("status"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "data1", "data2" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
}}

Try this
new MyAsyncTask.execute("http://10.10.10.10/data.php");
Declare the task as
class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
#Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
JSONObject json = JSONfunctions.getJSONfromURL(params[0]);
try {
JSONArray ip = json.getJSONArray("ip");
for (int i=0;i<ip.length();i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = ip.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("data1", e.getString("date"));
map.put("data2", "Location:" + e.getString("location") + " Status:" + e.getString("status"));
mylist.add(map);
}
return mylist
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}
#Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(YourActivity.this, result , R.layout.main,
new String[] { "data1", "data2" },
new int[] { R.id.item_title, R.id.item_subtitle });
YourActivity.this.setListAdapter(adapter); // If Activity extends ListActivity
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
Hope it helps.

Do not download any data in your onCreate() - if it takes too long then you will get ANR exception (Activity Not Responding). You should use AsyncTask as in your question. For AsyncTask you have very good example on android site:
http://developer.android.com/reference/android/os/AsyncTask.html
you should put JSONfunctions.getJSONfromURL() inside doInBackground()
and all whats below in onPostExecute()

Related

Android convert JSONObject to HashMap and display in ListView with SimpleAdapter

I try to search converting JSONObject to HashMap but most of the results are for Java not Android. Hence, I hope someone can share if you have experience in doing this before.
listview_with_simpleAdapter_and_hashmap.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
String[] food_id= new String[]{"1", "2", "3"};
String[] food_name = new String[]{"apple", "orange", "banana"};
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 3; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("ID", food_id[i]);
hm.put("Name", food_name[i]);
aList.add(hm);
}
String[] from = {"ID", "Name"};
int[] to = {R.id.text_id, R.id.text_name};
SimpleAdapter adapter = new SimpleAdapter(this, aList, R.layout.list_item, from, to);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
this file is working fine and simply display 2 columns in each row;
json.java
TextView mTxtDisplay;
String url = "http://192.168.1.103/web_service/omg.php/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTxtDisplay = (TextView) findViewById(R.id.tv);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
mTxtDisplay.setText(response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsObjRequest);
192.168.1.103/web_service/omg.php/
{
"32":"Western Food",
"35":"Japanese Food",
"37":"Italian Food"
}
JSON is working fine as well. The format is exactly the same as the ListView data -> ID and Name.
So my question is how to convert the JSONObject in omg.php to listview_with_simpleAdapter_and_hashmap.java ? I just need a simple example.
You could do something like this:
ListView listView = (ListView) findViewById(R.id.listView);
// ...
#Override
public void onResponse(JSONObject response) {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
try {
Iterator<String> iterator = response.keys();
while (iterator.hasNext()) {
String key = iterator.next();
String value = response.getString(key);
HashMap<String, String> map = new HashMap<>();
map.put(KEY_ID, key);
map.put(KEY_NAME, value);
list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
if(list.size() > 0) {
String[] from = {KEY_ID, KEY_NAME};
int[] to = {R.id.text_id, R.id.text_name};
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list,
R.layout.list_item, from, to);
listView.setAdapter(adapter);
}
}
try this code to convert Jsonobject to hashmap
Map<String, String> params = new HashMap<String, String>();
try
{
Iterator<?> keys = jsonObject.keys();
while (keys.hasNext())
{
String key = (String) keys.next();
String value = jsonObject.getString(key);
params.put(key, value);
}
}
catch (Exception xx)
{
xx.toString();
}

JSON parsing using AsynTask

i have done JSON parsing without using async task, it is working fine in gingerbread(2.3)
but when i am running the same project on ICS(ice cream sandwich) application is crashing, i heard somewhere that i will have to do the parsing in asynctask but i am new to android and unable to do that can someone help me ............
here's my JSON class :-
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}
and here's my MAinActivity which is using JSON class :-
public class MainActivity extends ListActivity {
Context context;
ArrayList<String> contentList,slugList;
ArrayList<HashMap<String, String>> mylist;
JSONObject json;
JSONArray latest;
ImageView bck;
String shareUrl;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
bck = (ImageView) findViewById(R.id.bckbtn);
mylist = new ArrayList<HashMap<String, String>>();
contentList=new ArrayList<String>();
slugList=new ArrayList<String>();
json = JSONfunctions.getJSONfromURL("http://madhuridixit-nene.com/wp/?json=get_category_posts&slug=latest");
context=this;
try{
latest = json.getJSONArray("posts");
for(int i=0;i<latest.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = latest.getJSONObject(i);
String name = e.getString("title_plain");
String content = e.getString("content");
contentList.add(content);
String chng = "–";
String fnl_Str = name.replace(chng, "");
String slug = e.getString("slug");
slugList.add(slug);
map.put("id", String.valueOf(i));
map.put("name", fnl_Str);
map.put("date", e.getString("date"));
shareUrl ="http://madhuridixit-nene.com/latest/post/";
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.activity_latest_tab,
new String[] { "name"},
new int[] { R.id.item_title});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position); */
//Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(context,WebViewActivity.class);
intent.putExtra("content", contentList.get(position));
intent.putExtra("shareUrl", shareUrl+slugList.get(position));
intent.putExtra("tab_value", 1);
startActivity(intent);
}
});
}
plz help me to get this working using AsyncTask .......
Change your code Using AsyncTask as for Getting data from server :
private class LongOperation extends AsyncTask<String, Void,
ArrayList<HashMap<String, String>>> {
ArrayList<String> contentList,slugList;
ArrayList<HashMap<String, String>> mylist;
JSONObject json;
JSONArray latest;
#Override
protected void onPreExecute() {
}
#Override
protected ArrayList<HashMap<String, String>>
doInBackground(String... params) {
mylist = new ArrayList<HashMap<String, String>>();
contentList=new ArrayList<String>();
slugList=new ArrayList<String>();
json = JSONfunctions.getJSONfromURL("http://madhuridixit-nene.com"+
"/wp/?json=get_category_posts&slug=latest");
try{
latest = json.getJSONArray("posts");
for(int i=0;i<latest.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = latest.getJSONObject(i);
String name = e.getString("title_plain");
String content = e.getString("content");
contentList.add(content);
String chng = "–";
String fnl_Str = name.replace(chng, "");
String slug = e.getString("slug");
slugList.add(slug);
map.put("id", String.valueOf(i));
map.put("name", fnl_Str);
map.put("date", e.getString("date"));
shareUrl ="http://madhuridixit-nene.com/latest/post/";
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return mylist;
}
#Override
protected void onPostExecute(
ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(this, result ,
R.layout.activity_latest_tab,
new String[] { "name"},
new int[] { R.id.item_title});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Intent intent=new Intent(MainActivity.this,WebViewActivity.class);
intent.putExtra("content", contentList.get(position));
intent.putExtra("shareUrl", shareUrl+slugList.get(position));
intent.putExtra("tab_value", 1);
startActivity(intent);
}
});
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
and execute AsyncTask from onCreate of Activity as:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
bck = (ImageView) findViewById(R.id.bckbtn);
new LongOperation().execute(""); // execute here
// your code here.....
finally you must read about AsyncTask for next use from here
http://developer.android.com/reference/android/os/AsyncTask.html
The problem is not with parsing of json data. The problem here is that you've been trying to make network connection on the Main thread. Network connections are long tasks and should be run in a separate thread. What you have to do here is basically put your
json = JSONfunctions.getJSONfromURL("http://madhuridixit-nene.com/wp/?json=get_category_posts&slug=latest");
line in the AsyncTask
I hope this helps!
First of all, consider to post Logcat output whenever you are facing issue/getting exceptions in Android.
Now, your current exception is NetworkOnMainThreadException, which is just because of you are making a web call directly on Main Thread.
From Android 3.0, Android has announced you can't do it directly on Main Thread.
To resolve this issue, you can either implement AsyncTask or write down the below code before making a web call:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Read more: Android StrictMode – NetworkOnMainThreadException
yes finally i have done it thanks for all the ideas ......
I have created another class for asynctask like this :-
class MyAsyncTask extends AsyncTask> > {
ArrayList> mylist = new ArrayList>();
#Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... params) {
// TODO Auto-generated method stub
mylist = new ArrayList<HashMap<String, String>>();
contentList=new ArrayList<String>();
slugList=new ArrayList<String>();
json = JSONfunctions.getJSONfromURL("http://madhuridixit-nene.com/wp/?json=get_category_posts&slug=latest");
try{
latest = json.getJSONArray("posts");
for(int i=0;i<latest.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = latest.getJSONObject(i);
String name = e.getString("title_plain");
String content = e.getString("content");
contentList.add(content);
String chng = "–";
String fnl_Str = name.replace(chng, "");
String slug = e.getString("slug");
slugList.add(slug);
map.put("id", String.valueOf(i));
map.put("name", fnl_Str);
map.put("date", e.getString("date"));
shareUrl ="http://madhuridixit-nene.com/latest/post/";
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
showProgress.setVisibility(View.VISIBLE);
return mylist;
}
#Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(LAtestTab.this, result , R.layout.activity_latest_tab,
new String[] { "name" },
new int[] { R.id.item_title});
LAtestTab.this.setListAdapter(adapter);// If Activity extends ListActivity
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
showProgress.setVisibility(View.GONE);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(context,WebViewActivity.class);
intent.putExtra("content", contentList.get(position));
intent.putExtra("shareUrl", shareUrl+slugList.get(position));
intent.putExtra("tab_value", 1);
startActivity(intent);
}
});
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
showProgress.setVisibility(View.VISIBLE);
}
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
showProgress.setVisibility(View.VISIBLE);
}
}

parsing image with json to ListView beside text

I'm working on an app which require to get the new titles and add thumb images beside them in listView and I don't know how to convet the photos url to images and put in the listView I made here's the code :
I can get the image url but I don't know what to do to add to the listView beside the text I got, any help ?
private class theJob extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>>{
#Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(MainActivity.this, result, R.layout.list_item,
new String[] {TAG_CAT_NAME }, new int[] {R.id.label });
setListAdapter(adapter);
Log.d("adapter", "works");
}
#Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... params) {
// TODO Auto-generated method stub
Log.d("Format", params[0]);
Log.d("URL", params[1]);
ArrayList<HashMap<String, String>> cat = new ArrayList<HashMap<String,String>>();
JsonParser jparser = new JsonParser();
Log.d("url", "to the other class");
JSONArray jArray = jparser.getJSONfronUrl(params[1]);
Log.d("json array", "created");
try{
for(int i=0 ; i< jArray.length() ; i++){
JSONObject joob = jArray.getJSONObject(i);
Log.d("jobj", "done");
String title = joob.getString(params[0]);
Log.d(TAG_CAT_NAME, "done");
cat_id = joob.getString(TAG_CAT_ID);
Log.d(TAG_CAT_ID, cat_id);
cat_url.add(i, joob.getString(TAG_CAT_URL)) ;
HashMap<String, String> map = new HashMap<String, String>();
map.put(params[0], title);
cat.add(map);
}
}catch(JSONException e){
e.printStackTrace();
}
Log.d("Going to ADAPTER", "working");
return cat;
}
}
Here is a googe example this will help you what you want :)
Update
Here you will fine more help about Lazy loading of images in list view.
You can make use of SmartImageView, it is a drop-in replacement for Android’s standard ImageView which additionally allows images to be loaded from URLs or the user’s contact address book. Images are cached to memory and to disk for super fast loading.
https://github.com/loopj/android-smart-image-view

android: update ListView with SimpleAdapter

I have a ListView with a SimpleAdapter. The Data for the ListView comes from json. I want to update the ListView every 5 min.
That works fine... But the ListView is allway a double. All items are to times in the ListView. Why? And wenn I update then 3 times....
I try
setListAdapter(null);
and
mylist.clear();
no effect
public class StartActivity extends ListActivity {
TextView text_1,text_2 ;
private Timer autoUpdate;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Button btnShowLocation;
// GPSTracker class
GpsData gps;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
btnShowLocation = (Button) findViewById(R.id.report_btn);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(StartActivity.this, ReportActivity.class);
startActivity(intent);
}
});
new task().execute();
}
#Override
public void onResume() {
super.onResume();
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
new task().execute();
}
});
}
}, 0, 300000); // updates each 5 min
}
class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(StartActivity.this);
InputStream is = null ;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Status Update...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface arg0) {
task.this.cancel(true);
}
});
}
#Override
protected Void doInBackground(String... params) {
//mylist.clear();
JSONObject json = jsonFunctions.getJSONfromURL("http://my.de", mlat, mlon);
try{
//String text = getString(R.string.report_TJ);
JSONArray earthquakes = json.getJSONArray("uTraf");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("first", "Stau: " + e.getString("road") + ", " + e.getString("county"));
map.put("second", e.getString("timestamp") + ", " + e.getString("suburb"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}
protected void onPostExecute(Void v) {
//setListAdapter(null);
ListAdapter adapter = new SimpleAdapter(StartActivity.this, mylist , R.layout.main,
new String[] { "first", "second" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(StartActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
this.progressDialog.dismiss();
}
}
#Override
public void onPause() {
autoUpdate.cancel();
super.onPause();
}
}
Your problem is with the call mylist.add(map); inside your for loop in doInBackground.
Your creating a map is fine but by calling the add function on mylist you are appending it to the list and not overriding the current map with the same keys. If you want to update existing items in the listview as opposed to just overwriting with fresh data then the mylist variable should probably be a HashMap also.
EDIT - Solution:
In doInBackground, just before you enter the loop to process data
(for(int i=0;i<earthquakes.length();i++))
call mylist.clear(). This will empty your array list before you start to add new data to it.
add mylist = new ArrayList<HashMap<String, String>>(); in doInBackGround() method
try{
//String text = getString(R.string.report_TJ);
JSONArray earthquakes = json.getJSONArray("uTraf");
mylist = new ArrayList<HashMap<String, String>>();
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("first", "Stau: " + e.getString("road") + ", " + e.getString("county"));
map.put("second", e.getString("timestamp") + ", " + e.getString("suburb"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}

Android ListView with or without Images

I am trying to create a listView where JSON data is pulled into a SimpleAdapter, but what i can figure out is how to hide the Rank image if it is returned null.
private class LoadDataTask extends AsyncTask<Void, Void, String[]> {
ProgressDialog Dialog = new ProgressDialog(Post.this);
#Override
protected void onPreExecute() {
super.onPreExecute();
mylist.clear();
Dialog.setMessage("Loading Posts...");
Dialog.setCancelable(true);
Dialog.show();
}
#Override
protected String[] doInBackground(Void... params) {
preferences = PreferenceManager.getDefaultSharedPreferences(Post.this);
String userID = preferences.getString("userID", "n/a");
String TID = Post.this.getIntent().getExtras().getString("id");
try{
JSONObject json = JSONfunctions.getJSONfromURL(Constants.BASE_URL+"/posts.php?user="+userID+"&postID="+TID);
JSONArray users = json.getJSONArray("users");
for(int i=0;i<users.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("pid", e.getString("poster_id"));
map.put("id", e.getString("post_id"));
map.put("Forum", e.getString("forum_id"));
map.put("Topic", e.getString("topic_id"));
map.put("Name", e.getString("poster_name"));
map.put("Text", string);
map.put("IP", e.getString("poster_ip"));
map.put("Status", e.getString("poster_status"));
map.put("Rank", e.getString("prank"));
map.put("Time", e.getString("poster_time"));
map.put("active", e.getString("active"));
map.put("posterisimage", e.getString("posterisimage"));
map.put("posterimage", e.getString("posterimage"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}
#Override
protected void onPostExecute(String[] result) {
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "Time", "Text", "Name" ,"Rank"}, <==Hide Rank if returned null ???
new int[] { R.id.item_date, R.id.item_subtitle , R.id.item_title, R.id.item_image});
((SimpleAdapter) adapter).notifyDataSetChanged();
setListAdapter(adapter);
Dialog.dismiss();
super.onPostExecute(result);
}
}
Can someone show or point in me the right direction.
You can write your own ViewBinder from SimpleAdapter:
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if(view.getId() != R.id.item_image)
return false;
if(data == null) {
view.setVisibility(View.GONE);
return true;
}
view.setVisibility(View.VISIBLE);
return false;
}
});
This ViewBinder is very primitive. I recommend customizing it to your particular layout.

Categories

Resources