2015-11-26 26 views
-1

我有一個自定義的ListView適配器,裏面有一些TextView。 你可以點擊一個TexView並點擊後,它啓動一個獲取一些數據的線程。我想在我點擊的TextView下添加接收的數據。 有沒有可能這樣做?你有什麼樣的例子嗎? 感謝如何更新ListView的一行,添加一個視圖到被點擊的項目?

編輯:

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

EDIT 2 我發現這裏http://www.survivingwithandroid.com/2013/01/android-expandablelistview-baseexpandablelistadapter.html

誰曾downvoted我的問題的完美解決方案,而無需編寫一個原因:我希望你有腹瀉-.-

+0

您是否使用持有人模式?你可以更新你的問題添加適配器代碼嗎? –

+0

已添加!是的,我正在使用ViewHolder模式。 – michoprogrammer

回答

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; 
     } 
    } 
+0

嗨阿什坎,我很抱歉,但您的解決方案無法正常工作。一個問題與接收ListView而不是簡單文本的數據有關,但主要問題是,當我收到數據時,我沒有引用,因此我不能執行您提出的if語句。 :-( – michoprogrammer

+0

你是什麼意思,你沒有參考? –

+0

在我收到的數據中,我沒有一個id用於做你提出的等於。:-( – michoprogrammer

相關問題