TextView does not update text inside an ArrayAdapter [closed]

1

I'm trying to update a text in a TextView inside the Adapter. The text is visually not appearing even using notifyDataSetChanged (). I use other visual forms such as enable disable components and they are working ok! On the TextView issue I correctly picked up the object and its value to add +1 and update the visual text, but it does not change.

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);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_pergunta, null);
        holder = new ViewHolder();

        Typeface tfLight = Typeface.createFromAsset(activity.getAssets(),
                "fonts/Oswald-Light.ttf");

        Typeface tfBold = Typeface.createFromAsset(activity.getAssets(),
                "fonts/Oswald-Bold.ttf");

        holder.tvLocal = (TextView) convertView
                .findViewById(R.id.tvLocal);
        holder.tvLocal.setTypeface(tfLight);

        holder.tvQuestion = (TextView) convertView
                .findViewById(R.id.tvQuestion);
        holder.tvQuestion.setTypeface(tfBold);

        holder.tvAnswer = (TextView) convertView
                .findViewById(R.id.tvAnswer);
        holder.tvAnswer.setTypeface(tfLight);

        holder.tvNumLike = (TextView) convertView
                .findViewById(R.id.tvNumLike);
        holder.tvNumLike.setTypeface(tfLight);

        holder.tvNumDisLike = (TextView) convertView
                .findViewById(R.id.tvNumDisLike);
        holder.tvNumDisLike.setTypeface(tfLight);

        holder.llContainer = (LinearLayout) convertView.findViewById(R.id.llContainer);
        holder.llContainerLikeDislike = (LinearLayout) convertView.findViewById(R.id.llContainerLikeDislike);
        holder.llLike = (LinearLayout) convertView.findViewById(R.id.llLike);
        holder.llDisLike = (LinearLayout) convertView.findViewById(R.id.llDisLike);

        holder.llContainer.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                int visibility = v.findViewById(R.id.llContainerLikeDislike).getVisibility();

                if(visibility == View.GONE){
                    UtilsLab.expand(v.findViewById(R.id.llContainerLikeDislike));
                } else {
                    UtilsLab.collapse(v.findViewById(R.id.llContainerLikeDislike));
                }
                v.findViewById(R.id.llContainerLikeDislike).requestLayout();
                notifyDataSetChanged();

                if(UtilsLab.hasIdUpDownVote(activity, String.valueOf(qa.getId()))) {
                    v.findViewById(R.id.llLike).setEnabled(false);
                    v.findViewById(R.id.llDisLike).setEnabled(false);
                }
            }
        });

        holder.llLike.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                UpDownVoteWsAsyncTask asyncTask = new UpDownVoteWsAsyncTask();
                asyncTask.flagUPDown = 1;
                asyncTask.id = qa.getId();
                asyncTask.tvNumUpdate = (TextView) v.findViewById(R.id.tvNumLike);
                asyncTask.llContainerDisable1 = (LinearLayout) v;
                asyncTask.llContainerDisable2 = (LinearLayout) ((LinearLayout) v.getParent()).findViewById(R.id.llDisLike);
                asyncTask.execute(ConstantsLab.WS_UP_DOWN_VOTE);
            }
        });

        holder.llDisLike.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                UpDownVoteWsAsyncTask asyncTask = new UpDownVoteWsAsyncTask();
                asyncTask.flagUPDown = 0;
                asyncTask.id = qa.getId();
                asyncTask.tvNumUpdate = (TextView) v.findViewById(R.id.tvNumDisLike);
                asyncTask.llContainerDisable1 = (LinearLayout) v;
                asyncTask.llContainerDisable2 = (LinearLayout) ((LinearLayout) v.getParent()).findViewById(R.id.llLike);
                asyncTask.execute(ConstantsLab.WS_UP_DOWN_VOTE);
            }
        });

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    String name = qa.getName();
    String city = qa.getCity();
    String state = qa.getState();

    if(qa.getName().equals(null) || qa.getName().equals("null")) {
        name = " ";
    }

    if(qa.getCity().equals(null) || qa.getCity().equals("null")) {
        city = " ";
    }

    if(qa.getState().equals(null) || qa.getState().equals("null")) {
        state = " ";
    }

    holder.tvLocal.setText(activity.getString(R.string.local_item_pergunta, name, city, state));

    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) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<QA> list = arrayQA;

            int count = list.size();
            final ArrayList<QA> nlist = new ArrayList<QA>(count);

            QA filterable;

            for (int i = 0; i < count; i++) {
                filterable = list.get(i);
                String questionFiltred = filterable.getQuestion();

                if (questionFiltred.toLowerCase().contains(filterString)) {
                    nlist.add(filterable);
                }

            }

            results.values = nlist;
            results.count = nlist.size();

            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);

                                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;

        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {

                new AlertDialog.Builder(activity)
                        .setTitle(activity.getString(R.string.warnning))
                        .setMessage(error)
                        .setCancelable(false)
                        .setPositiveButton(R.string.ok,
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        dialog.dismiss();
                                    }
                                }).create().show();

            }
        });
    }

}

}

    
asked by anonymous 17.06.2015 / 14:32

1 answer

0

Is this part that you add to the ArrayAdapter?

/**
 * 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();
        }
    });
}

If it is, you are actually adding it to the list, not ArrayAdapter:

QuestionsAnswersAdapter.this.add(qa);
    
01.07.2015 / 21:12