如何更新ListView的行,将View添加到单击的项目?

时间:2015-11-26 17:05:54

标签: android android-listview custom-adapter

我有一个自定义的ListView适配器,里面有一些TextView。 您可以单击TexView,单击后,它会启动一个获取某些数据的线程。我想在我单击的TextView下准确添加收到的数据。 有可能这样做吗?你有什么例子吗? 感谢

编辑:

public class MyAdapter extends ArrayAdapter<MyObject> implements TaskListener, View.OnClickListener {
    private final static DateFormat originalFormat = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
    private final static DateFormat targetFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());

    private int resource;
    private Activity activity;
    private ViewHolder holder;
    private User user;
    private ArrayList<MyObject> myobjReplies;

    public MyAdapter(Activity activity, int resource,
                            ArrayList<MyObject> myobj, User user) {
        super(activity, resource, myobj);
        this.activity = activity;
        this.resource = resource;
        this.user = user;
    }

    static class ViewHolder {
        TextView date;
        ImageView avatar;
        TextView title;
        TextView subTitle;
        TextView text;
        ListView attachments;
        TextView responseNumber;
        ListView repliesList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    resource, parent, false);
            holder = new ViewHolder();

            holder.date = (TextView) convertView.findViewById(R.id.myobj_date);

            holder.avatar = (ImageView) convertView.findViewById(R.id.myobj_avatar);

            holder.title = (TextView) convertView.findViewById(R.id.myobj_title);

            holder.subTitle = (TextView) convertView.findViewById(R.id.myobj_sub_title);

            holder.text = (TextView) convertView.findViewById(R.id.myobj_text);

            holder.attachments = (ListView) convertView.findViewById(R.id.attachments_links);

            holder.responseNumber = (TextView) convertView.findViewById(R.id.response_number);

            convertView.setTag(holder);
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();

        final MyObj estimate = getItem(position);

        holder.date.setText(myobj.getCreationTime());


        holder.title.setText(activity.getResources().getString(R.string.one_myobj_title));


        if (myobj.getStatus().equals("ACTIVE")) {
            holder.avatar.setBackgroundResource(R.drawable.myobj_active);
        } else if (myobj.getStatus().equals("REFUSED")) {
            holder.avatar.setBackgroundResource(R.drawable.myobj_refused);
        } else if (myobj.getStatus().equals("CLOSED")) {
            holder.avatar.setBackgroundResource(R.drawable.myobj_closed);
        } else if (myobj.getStatus().equals("EXPIRED")) {
            holder.avatar.setBackgroundResource(R.drawable.myobj_espired);
        }

        holder.subTitle.setText(getMyObjOptions(estimate.getOptions()));

        holder.text.setText(myobj.getText());

        if (estimate.getHasAttachment()) {
            AttachmentsAdapter attachmentsAdapter = new AttachmentsAdapter(activity, R.layout.component_attachment, attachmentsClickable(estimate.getAttachments(), myobj.getQid() != null ? myobj.getQid() : myobj.getRandomId(), myobj.getUserId()));
            holder.attachments.setAdapter(attachmentsAdapter);
        }

        ArrayList<String> responseList = myobj.getResponseList();
        StringBuilder sb = new StringBuilder();
        if (Utils.isNotEmpty(responseList)) {
            holder.responseNumber.setText(sb.append(activity.getResources().getString(R.string.response_number)).append(" ").append(responseList.size()).toString());
            holder.responseNumber.setTag(R.id.myobj_qid, myobj.getQid());
            holder.responseNumber.setOnClickListener(this);
        } else {
            holder.responseNumber.setText(sb.append(activity.getResources().getString(R.string.response_number)).append(" 0").toString());
        }

        if (Utils.isNotEmpty(myobjReplies)) { //here is the point where I check if I received the data
            MyObjRepliesAdapter repliesAdapter = new MyObjRepliesAdapter((MyActivity) activity,
                    R.layout.component_reply, myobjReplies);
            ((ListView) convertView.findViewById(R.id.replies_list)).setAdapter(repliesAdapter);
        }

        return convertView;
    }

    private ArrayList<String> attachmentsClickable(ArrayList<String> attachments, String attachmentId, String uid) {
        ArrayList<String> attachList = new ArrayList<String>();
        int i = 1;
        for (String link : attachments) {
            StringBuilder urlBuilder = new StringBuilder();
            urlBuilder.append("?fk=").append(link).append("&qid=").append(attachmentId).append("&n=").append(i++).append("&uid=").append(uid);
            attachList.add(urlBuilder.toString());
        }
        return attachList;
    }

    private String getMyObjOptions(ArrayList<String> options) {
        String res = options.toString();
        return res.substring(1, res.length() - 1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.response_number:
                Request request = new Request(Request.MYOBJ_REPLIES);
                request.setParam("accesstoken", user.getAccessToken());
                request.setParam("requestid", (String) v.getTag(R.id.myobj_qid));
                new Mwrapper(false, activity).addListener(this).process(request);
                break;
        }
    }

    @Override
    public void onTaskCompleted(Request request, String data) { //getting the data
        Response response = null;
        try {
            if (request.getAction().equals(Request.MYOBJ_REPLIES)) {
                response = Response.fromString(data);
                myobjReplies = response.getMyobjs();

                notifyDataSetChanged();
            }

        } catch (Exception e) {
            Toast.makeText(
                    activity,
                    activity.getResources().getString(
                            R.string.search_error), Toast.LENGTH_SHORT).show();
            return;
        }
    }

    @Override
    public void onTaskFailed(Request request) {
    }
}

编辑2 我在这里找到了一个完美的解决方案http://www.survivingwithandroid.com/2013/01/android-expandablelistview-baseexpandablelistadapter.html

谁没有写出理由而对我的问题进行了投票:我希望你得了腹泻-.-

1 个答案:

答案 0 :(得分:0)

将onClick更改为:

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.response_number:

                final ViewHolder holder = (ViewHolder) v.getTag();
                Request request = new Request(Request.MYOBJ_REPLIES);
                request.setParam("accesstoken", user.getAccessToken());
                request.setParam("requestid", (String) v.getTag(R.id.myobj_qid));
                Mwrapper wrapper = new Mwrapper(false, activity).addListener(new TaskListener(){

@Override
public void onTaskCompleted(Request request, String data) { //getting the data
    Response response = null;
    try {
        if (request.getAction().equals(Request.MYOBJ_REPLIES)) {
            response = Response.fromString(data);
            myobjReplies = response.getMyobjs();

            if(holder.responseNumber.getTag().equals(request.getId())) //param requestid
              holder.responseResult.setText(the result string); //responseResult is an empty textview

        }

    } catch (Exception e) {
        Toast.makeText(
                activity,
                activity.getResources().getString(
                        R.string.search_error), Toast.LENGTH_SHORT).show();
        return;
    }
}

@Override
public void onTaskFailed(Request request) {
}



});


wrapper.process(request);
                break;
        }
    }