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

Posted

技术标签:

【中文标题】如何更新 ListView 的一行,将视图添加到单击的项目?【英文标题】:How to update a row of a ListView adding a View to the item clicked? 【发布时间】:2016-02-29 20:39:19 【问题描述】:

我有一个自定义 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

谁在没有写出理由的情况下否决了我的问题:我希望你得了腹泻-.-

【问题讨论】:

您使用的是支架模式吗?你能更新你的问题,添加适配器代码吗? 已添加!是的,我正在使用 ViewHolder 模式。 【参考方案1】:

将您的 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;
        
    

【讨论】:

您好 Ashkan,很抱歉,您的解决方案不起作用。一个问题与接收的数据有关,它是 ListView 而不是简单的文本,但主要问题是当我收到数据时我没有参考,所以我无法执行您提出的 if 语句。 :-( 没有参考资料是什么意思? 在我收到的数据中,我没有用于执行您建议的 equals 的 id。 :-( 在我定义持有者并比较该 id 的地方使 id 也是最终的。 感谢 Ahskan,我发现这个解决方案 survivingwithandroid.com/2013/01/… 非常适合!

以上是关于如何更新 ListView 的一行,将视图添加到单击的项目?的主要内容,如果未能解决你的问题,请参考以下文章

Android ListActivity - 如何在 ListView 下方添加视图?

从ArrayAdapter每毫秒更新Android ListView中的一行

滚动时更新自定义列表视图失败

如何在列表视图顶部添加一行

在 Listview 的第一行添加 ViewFlipper

如何使用 AsyncTask 更新全局变量