无法在另一个函数中访问customArrayadapter中的viewHolder?

时间:2017-06-29 13:04:07

标签: android android-arrayadapter

我正在创建一个带有列表的应用程序,该列表根据服务器的值列出卡片。 我创建了一个StudentCardArrayAdapter来实现这一点,一切正常。所有数据都已填入卡片列表中。我也可以分别在每张卡片上点击按钮获取值。

我需要的是点击按钮,它会异步调用方法请求服务器获取数据,并从服务器获取一个值,根据该值,我需要更改该特定卡中的按钮文本。

我的StudentCardArrayAdapter代码:

public class StudentCardArrayAdapter extends ArrayAdapter<StudentCard> {
    private static final String TAG = "CardArrayAdapter";
    private List<StudentCard> cardList = new ArrayList<StudentCard>();
    private Context mContext;
    String selected = "0";
    PreferenceHelper prefs;
    CardViewHolder viewHolder;
    View row;
    ProgressDialog pd;
    static class CardViewHolder {
        TextView studentname;
        TextView stop;
        Button selectbutton;
        CircleImageView imageId;

    }

    public StudentCardArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        this.mContext = context;
        prefs = new PreferenceHelper(this.mContext);
        pd = new ProgressDialog(this.mContext);
    }

    @Override
    public void add(StudentCard object) {
        cardList.add(object);
        super.add(object);
    }

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

    @Override
    public StudentCard getItem(int index) {
        return this.cardList.get(index);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.student_card, parent, false);
            viewHolder = new CardViewHolder();
            viewHolder.studentname = (TextView) row.findViewById(R.id.studentname);
            viewHolder.stop = (TextView) row.findViewById(R.id.stop);
            viewHolder.selectbutton = (Button) row.findViewById(R.id.selectbutton);
            viewHolder.imageId = (CircleImageView) row.findViewById(R.id.imageId);
            row.setTag(viewHolder);
        } else {
            viewHolder = (CardViewHolder)row.getTag();
        }
        StudentCard card = getItem(position);

        viewHolder.studentname.setText(card.getStudName());
        viewHolder.studentname.setTextColor(Color.parseColor("#000000"));

        viewHolder.stop.setText(card.getStudStop());
        viewHolder.stop.setTextColor(Color.parseColor("#000000"));

        if(card.getSelected().equals("1")){
            viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.selected));
            viewHolder.selectbutton.setEnabled(false);
        }
        else{
            viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.select));
            viewHolder.selectbutton.setEnabled(true);
        }

        final String studid = card.getStudId();
        final String busname = prefs.getString("busname", "0");
        final String schoolid = prefs.getString("schoolid", "");
        viewHolder.selectbutton.setOnClickListener(new View.OnClickListener()
        {
            String updatedvalue = "0";
            @Override
            public void onClick(View v)
            {
                Log.e("studid",studid);
                Log.e("busname",busname);
                Log.e("schoolid",schoolid);
                    selectstudent(v, studid, busname, schoolid,mContext);
                //Toast.makeText(v.getContext(), amountinfo, Toast.LENGTH_SHORT).show();
                /*SnackbarManager.show(Snackbar.with(this) // context
                        .text(amountinfo));*/
            }
        });
        Picasso.with(mContext).load(card.getImageUrl()).fit().error(R.mipmap.ic_launcher).into(viewHolder.imageId);
        return row;
    }


    public void selectstudent(final View v, String studid, String busname, String schoolid, final Context mContext) {
        String returnedselected = "0";
        Log.e("BASE_URL_STUDENT_UPDATE", Constants.BASE_URL_STUDENT_UPDATE + "?studid=" + studid+"&busname="+busname+"&schoolid="+schoolid);
        RestClientHelper.getInstance().get(Constants.BASE_URL_STUDENT_UPDATE + "?studid=" + studid+"&busname="+busname+"&schoolid="+schoolid, new RestClientHelper.RestClientListener() {
            @Override
            public void onSuccess(String response) {
                Log.e("RESULT", response);
                try {

                    JSONObject result = new JSONObject(response);
                    JSONArray posts = result.optJSONArray("status");
                    for (int i = 0; i < posts.length(); i++) {
                        JSONObject post = posts.optJSONObject(i);
                        String status = post.optString("status");
                        if (status.equals("true")) {
                             selected = post.optString("selected");
                        } else {
                             selected = post.optString("selected");
                            String error = post.optString("error");
                            SnackbarManager.show(Snackbar.with(getContext()) // context
                                    .text(error));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                } catch (Exception e) {

                } finally {
                    if(selected.equals("1")){
                        viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.selected));
                        viewHolder.selectbutton.setEnabled(false);
                    }

                }
            }

            @Override
            public void onError(String error) {
                Log.e("error", error);
                selected = "0";
            }
        });
    }
}

我使用了以下代码,但没有任何效果..也没有错误..而且没有更改按钮文本。我从服务器获取的值为1。

if(selected.equals("1")){
                        viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.selected));
                        viewHolder.selectbutton.setEnabled(false);
                    }

我是android新手,并且被困在这里。请帮帮我。

最终工作

随着Krish提到的变化,我更新了他建议的代码。 并在onClick中添加了这些更改

if(card.getSelected().equals("1")){
                    viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.selected));
                    viewHolder.selectbutton.setEnabled(false);
                }

1 个答案:

答案 0 :(得分:0)

更改此代码,

 public void selectstudent(StudentCard card, String studid, String busname, String schoolid, final Context mContext) {
        String returnedselected = "0";
        Log.e("BASE_URL_STUDENT_UPDATE", Constants.BASE_URL_STUDENT_UPDATE + "?studid=" + studid+"&busname="+busname+"&schoolid="+schoolid);
        RestClientHelper.getInstance().get(Constants.BASE_URL_STUDENT_UPDATE + "?studid=" + studid+"&busname="+busname+"&schoolid="+schoolid, new RestClientHelper.RestClientListener() {
            @Override
            public void onSuccess(String response) {
                Log.e("RESULT", response);
                try {

                    JSONObject result = new JSONObject(response);
                    JSONArray posts = result.optJSONArray("status");
                    for (int i = 0; i < posts.length(); i++) {
                        JSONObject post = posts.optJSONObject(i);
                        String status = post.optString("status");
                        if (status.equals("true")) {
                            selected = post.optString("selected");
                        } else {
                            selected = post.optString("selected");
                            String error = post.optString("error");
                            SnackbarManager.show(Snackbar.with(getContext()) // context
                                    .text(error));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                } catch (Exception e) {

                } finally {
                    if(selected.equals("1")){
                        card.setSelected("1");
                         notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onError(String error) {
                Log.e("error", error);
                selected = "0";
            }
        });
    }

并改变这一行,

final StudentCard card = getItem(position);

并在onclick中调用方法。

selectstudent(card, studid, busname, schoolid,mContext);