0

我正在学习如何使用 AsyncTask 更改单项自定义 listview android,我已按照本教程进行操作。

但这在我的列表视图中似乎不起作用。

这是我的代码

private class ViewHolder {
    public ImageButton favorite;
    public TextView jmlh_favorite;
    protected int position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    final ViewHolder holder;

    if (convertView == null) {
        vi = inflater.inflate(R.layout.laporan_list_item, null);
        holder = new ViewHolder();
        holder.jmlh_favorite = (TextView)vi.findViewById(R.id.jmlh_favorite);
        holder.favorite = (ImageButton)vi.findViewById(R.id.favorite);
        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();
    }

    holder.favorite.setOnClickListener(new android.view.View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.position = position;
            position_change=position;
            String varid_laporan = holder.id_laporan.getText().toString();
            String var_kat_favorite = holder.kat_favorite.getText().toString();
            url = "http://xxxx.com/android/upload_image/favorite_laporan.php?kat="+var_kat_favorite+"&id_laporan="+varid_laporan+"&uid="+URLEncoder.encode(uid);
            grabURL(url,position,holder); 
        } 
    });

    return vi;
}

public void grabURL(String url, int position,ViewHolder holder) {
    Log.v("Android Spinner JSON Data Activity", url);
    mTask = new GrabURL(position, holder).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
}

private class GrabURL extends AsyncTask<String, Void, String> {
    private static final int REGISTRATION_TIMEOUT = 3 * 1000;
    private static final int WAIT_TIMEOUT = 30 * 1000;
    private final HttpClient httpclient = new DefaultHttpClient();
    final HttpParams params = httpclient.getParams();
    HttpResponse response;
    private String content = null;
    private boolean error = false;
    private ProgressDialog dialog = new ProgressDialog(activity);

    private int mPosition;
    private ViewHolder mHolder;

    public GrabURL(int position, ViewHolder holder) {
        mPosition = position;
        mHolder = holder;
    }

    protected void onPreExecute() {
        dialog.setMessage("Getting your data... Please wait...");
    }

    protected String doInBackground(String... urls) {
        String URL = null;

        try {
            URL = urls[0];
            HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
            ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);

            HttpPost httpPost = new HttpPost(URL);
            response = httpclient.execute(httpPost);

            StatusLine statusLine = response.getStatusLine();

            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                content = out.toString();
            } else {
                //Closes the connection.
                Log.w("HTTP1:",statusLine.getReasonPhrase());
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            Log.w("HTTP2:",e );
            content = e.getMessage();
            error = true;
            cancel(true);
        } catch (IOException e) {
            Log.w("HTTP3:",e );
            content = e.getMessage();
            error = true;
            cancel(true);
        } catch (Exception e) {
            Log.w("HTTP4:",e );
            content = e.getMessage();
            error = true;
            cancel(true);
        }

        return content;
    }

    protected void onCancelled() {
        mTask.cancel(true);
        statusKoneksi();
    }

    protected void onPostExecute(String content) {
        if (error) {
            mTask.cancel(true);

            statusKoneksi();
        } else {
            displaylaporanList(content,mPosition,mHolder);
            mTask.cancel(true);
        }
    }
}

private void displaylaporanList(String response, int mPosition, ViewHolder mHolder){

    JSONObject json = null; 

    try {
        json = new JSONObject(response); 
        laporanListObj = json.getJSONArray(ContentLaporanActivity.TAG_FAVORITE_LAPORAN);
        int Jumlah_list_Data = laporanListObj.length();

        if (Jumlah_list_Data== 0) {
        } else {
            JSONObject c = laporanListObj.getJSONObject(0);
            String jumlah_favorite = c.getString(ContentLaporanActivity.TAG_BANYAK_FAVORITE_LAPORAN);
            String status_favorite = c.getString(ContentLaporanActivity.TAG_STATUS_FAVORITE);

            if (mHolder.position == mPosition) {
                mHolder.jmlh_favorite.setText(status_favorite);
            }
        }

        notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

void statusKoneksi(){
    Toast.makeText(activity, "gagal", Toast.LENGTH_SHORT).show();
}

这部分我想在方法displaylaporanList()中的结果异步任务时设置文本,但它没有设置文本。

if (mHolder.position == mPosition) {
    mHolder.jmlh_favorite.setText(status_favorite);
}

如何更新文本?

4

1 回答 1

0

您的代码的逻辑问题如下:您不应该在getView(). ViewHolder只是为了使列表性能更好,不应在任何时候用作列表项的参考。

就您而言,要解决此问题,您应该holder.jmlh_favorite.setText(status_favorite)在内部调用getView()(例如,将位置和文本存储在某处)。从displaylaporanList()您只需要调用notifyDataSetChanged()来通知适配器数据已更改并应更新列表项。此外,有关ListViews.

于 2013-10-24T09:30:07.397 回答