listview not refresh from asyncktask

106 views Asked by At

Hey all im trying to update custom listview from asyncktask i have 2 different layuots 1 my main and the other is for 1 row. i have also 4 class the main activity,the adaptar class,the asyncktask ,and the Entry which is one object. i notice somthing weird that when i use breakpoint on the onpost method everything works fine it load my list but if i dont ist not working thx alow

public class connect_Async_Task extends AsyncTask<String,ArrayList<Entry>,ArrayList<Entry>> {
    private Activity mContext;
    private ProgressDialog progressDialog ;
    custom_list adapter;
    ArrayList<Entry> EntryArray=new ArrayList<Entry>();
    connect_Async_Task(Activity context,custom_list adapter1)
    {
        mContext=context;
        adapter=adapter1;
    }
    //ProgressDialog dialog = new ProgressDialog(getParent(),R.style.progressdialog);
    // The variable is moved here, we only need it here while displaying the
    // progress dialog.
    TextView txtView;
    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage("Loading preview...");
        if(progressDialog != null) progressDialog.show();
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface arg0) {
                connect_Async_Task.this.cancel(true);
            }
        });
    }
    @Override
    protected ArrayList<Entry> doInBackground(String... uri) {
        android.os.Debug.waitForDebugger();
        HttpClient httpclient = new DefaultHttpClient();
        // Prepare a request object
        HttpGet httpget = new HttpGet(uri[0]);
        // Execute the request
        HttpResponse response;
        String result;
        result = null;
        try {
            response = httpclient.execute(httpget);
            // Examine the response status
            // Get hold of the response entity
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release
            if (entity != null) {
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
                // now you have the string representation of the HTML request
                instream.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONArray json_Array;

        try {
            json_Array = new JSONArray(result);
            for (int i = 0; i < json_Array.length(); i++) {
                JSONObject json_data = json_Array.getJSONObject(i);
                Integer jsonIDs = json_data.getInt("ID");
                String jsonNames = json_data.getString("Name");
                //String jsonImage =  json_data.getString("Image");
                String jsonCategory = json_data.getString("Category");
                String jsonIngredients = json_data.getString("Ingredients");
                String jsonPrice = json_data.getString("Price");
                Entry k=new Entry(jsonIDs,jsonNames,jsonCategory,jsonIngredients,jsonPrice);
                EntryArray.add(i,k);                    
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return EntryArray;
    }
    @Override
    protected void onPostExecute(ArrayList<Entry> EntryArray) {
        this.progressDialog.dismiss();
        adapter.upDateEntries(EntryArray);
    }
}

and the main activity

public class MainActivity extends ActionBarActivity  {

ListView list;
custom_list madapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list=(ListView)findViewById(R.id.listView);
    madapter=new custom_list(this);
    list.setAdapter(madapter);

    connect_Async_Task conn = new connect_Async_Task(this,madapter);
    conn.execute("string that return jason array");     
}

Edit - the Adapter:

package com.example.app;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class custom_list extends BaseAdapter {

private Context mContext;

private LayoutInflater mLayoutInflater;

private ArrayList<Entry> mEntries = new ArrayList<Entry>();

public custom_list(Context context) {
    mContext = context;
    mLayoutInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
}

@Override
public int getCount() {
    return mEntries.size();
}

@Override
public Object getItem(int position) {
    return mEntries.get(position);
}

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

@Override
public View getView(int position, View convertView,
                    ViewGroup parent) {
    RelativeLayout itemView;
    if (convertView == null) {
        itemView = (RelativeLayout) mLayoutInflater.inflate(
                R.layout.fragment_custom, parent, false);
    } else {
        itemView = (RelativeLayout) convertView;
    }
    TextView titleText = (TextView)
            itemView.findViewById(R.id.listTitle);
    TextView descriptionText = (TextView)
            itemView.findViewById(R.id.listDescription);
    String title = mEntries.get(position).getJsonNames();
    titleText.setText(title);
    String description =
            mEntries.get(position).getJsonNames();
    if (description.trim().length() == 0) {
        description = "Sorry, no description for this image.";
    }
    descriptionText.setText(description);
    return itemView;
}

public void upDateEntries(ArrayList<Entry> entries) {
    mEntries.clear();
    mEntries.addAll(entries);
    notifyDataSetChanged();
}

}

1

There are 1 answers

0
LeoFarage On

I can't comment yet. But, you don't really need to set the adapter again on the ListView (onPostExecute() of the AsyncTask). Just using the notifyDataSetChanged() should be working.

Well, try to remove the ListView k = (ListView)mContext.findViewById(R.id.listView); (and it's subsequent call on the method) and see if it works.

PS: take a look at Retrofit for working with your JSON requests.