TextView未更新适配器中的文本

时间:2015-06-18 01:07:33

标签: android android-listview android-arrayadapter

我正在尝试更新适配器内TextView中的文本。即使使用notifyDataSetChanged(),也不会直观地显示/更新文本。使用其他可视化表单,如启用和禁用组件正常工作!在TextView问题上正确选取对象及其值以添加+1并更新可视文本,但不会改变。

public class QuestionsAnswersAdapter extends ArrayAdapter<QA> implements Filterable {

private List<QA>filteredData = null;

private ArrayList<QA> arrayQA;
private ViewHolder holder;
private final LayoutInflater inflater;
private Activity activity;

public PerguntasRespostasAdapter(Activity activity,
        ArrayList<QA> arrayQA) {
    super(activity, 0);

    this.inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.arrayQA = arrayQA;
    this.filteredData = arrayQA; 
    this.activity = activity;
}

/**
 * Stores the visual components for better performance.
 */
static class ViewHolder {
    private TextView tvLocal, tvQuestion, tvAnswer, tvNumLike, tvNumDisLike;
    private LinearLayout llContainerLikeDislike, llContainer, llLike, llDisLike;
}

/**
 * Return the size of {@code arrayQA}.
 */
@Override
public int getCount() {
    return filteredData.size();
}

/**
 * Return {@link QA}.
 * @param position
 * @return {@link QA}
 */
public QA getItemQa(int position) {
    return filteredData.get(position);
}

/**
 * Insert {@link QA} in the {@code arrayQA}.
 * @param qa: {@link QA}
 */
public void addNewQA(final QA qa) {
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            filteredData.add(qa);
            notifyDataSetChanged();
        }
    });
}

/**
 * Return the {@code id} of the {@link QA}.
 */
@Override
public long getItemId(int position) {
    return filteredData.get(position).getId();
}

/**
 * Return line of the {@link BaseAdapter}.
 */
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {

    final QA qa = filteredData.get(position);

    ...

    holder.tvQuestion.setText(qa.getQuestion());
    holder.tvAnswer.setText(qa.getAnswer());

    holder.tvNumLike.setText(String.valueOf(qa.getUp_count()));
    holder.tvNumDisLike.setText(String.valueOf(qa.getDown_count()));

    return convertView;
}

public Filter getFilter() {

    Filter mFilter = new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            ...

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                final FilterResults results) {
            filteredData = (ArrayList<QA>) results.values;
            notifyDataSetChanged();
        }

    };

    return mFilter;
}

/**
 * {@link AsyncTask} for the send data like or dislike.
 */
public class UpDownVoteWsAsyncTask extends AsyncTask<String, Integer, Boolean>{

    private ProgressDialog progress;
    private int id, flagUPDown;
    private String flag;
    private boolean isError;
    private TextView tvNumUpdate;
    private LinearLayout llContainerDisable1, llContainerDisable2;

    private void closesProgress() {
        progress.dismiss();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress = ProgressDialog.show(activity, "", activity.getString(R.string.loading_send), true);
    }

    @SuppressWarnings({ "deprecation" })
    @Override
    protected Boolean doInBackground(String... params) {
        if(params.length > 0) {

            if(flagUPDown == 0) {
                flag = "down";
            } else {
                flag = "up";
            }

            String url = params[0] + id + "/" + flag;
            HttpClient httpClient = new DefaultHttpClient();

            HttpPut httpPut = new HttpPut(url);

            httpPut.setHeader(ConstantsLab.ACCEPT, ConstantsLab.APPLICATION_JSON);
            httpPut.setHeader(ConstantsLab.CONTENT_TYPE, ConstantsLab.APPLICATION_JSON_CHARSET_UTF_8);

            try {
                HttpResponse response = httpClient.execute(httpPut);
                if (response != null) {
                    if (response.getStatusLine().getStatusCode() == 200) {

                        UtilsLab.setIdUpDownVote(activity, String.valueOf(id));

                        activity.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                String numString = tvNumUpdate.getText().toString();

                                String newNumber = String.valueOf(Integer.parseInt(numString) + 1);

                                /***********************************************
                                *
                                *
                                * This is the part of update
                                *
                                *
                                ***********************************************/

                                tvNumUpdate.setText(newNumber);

                                llContainerDisable1.setEnabled(false);
                                llContainerDisable2.setEnabled(false);
                                notifyDataSetChanged();
                            }
                        });


                    } else {
                        showErrorDialog(activity
                                .getString(R.string.erro_ws));
                    }
                } else {
                    showErrorDialog(activity.getString(R.string.erro_ws));
                }
            } catch (IOException e) {
                e.printStackTrace();
                showErrorDialog(activity.getString(R.string.erro_ws));
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        closesProgress();
    }

    private void showErrorDialog(final String error) {
        isError = true;

       ...
    }

}

0 个答案:

没有答案