2011-11-24 62 views
1


我有一個ArrayAdapter的列表視圖。在一個位置的ListView使用某個ArrayList中的相同位置填充自身。在每個listitem 有兩個按鈕。當onClickListener啓動時,它得到 覆蓋的View getView(position,View等)的位置,從ArrayList中取出一些變量並將其發送到數據庫。問題是,這個位置並不準確。有時我會在我想要的Listitem上方獲得Listitem的位置。這裏是代碼:列表視圖陣列適配器位置onClick

public class MemoAdapter extends ArrayAdapter<String> { 

    private final Activity context; 
    ArrayList<String> commentarray; 
    ArrayList<String> fromarray; 
    ArrayList<String> datearray; 
    ArrayList<String> ids; 
    ArrayList<String> feedback; 
    Dialog dialog; 
    EditText feedbacktxt; 
    String token; 
    String website; 

    public MemoAdapter(Activity context, ArrayList<String> commentArray, 
      ArrayList<String> fromArray, ArrayList<String> dateArray, 
      ArrayList<String> ids, ArrayList<String> feedback, String token, 
      String website) { 
     super(context, R.layout.memo_listitem, commentArray); 
     this.context = context; 
     this.commentarray = commentArray; 
     this.fromarray = fromArray; 
     this.datearray = dateArray; 
     this.ids = ids; 
     this.feedback = feedback; 
     this.token = token; 
     this.website = website; 
    } 
    @Override 
    public long getItemId(int position) { 

     return 0; 
    } 
    // static to save the reference to the outer class and to avoid access to 
    // any members of the containing class 
    static class ViewHolder { 

     public TextView textView; 
     public TextView textView2; 
     public TextView textView3; 
     public TextView textView4; 
     // public ImageView reply; 
     // public ImageView accept; 
     public Button reply; 
     public Button accept; 
     public RelativeLayout r; 
    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     final ViewHolder holder; 
     View rowView = convertView; 

     if (rowView == null) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      rowView = inflater.inflate(R.layout.memo_listitem, null); 
      holder = new ViewHolder(); 

      holder.textView = (TextView) rowView 
        .findViewById(R.id.memodescription); 
      holder.textView2 = (TextView) rowView.findViewById(R.id.memofrom); 
      holder.textView3 = (TextView) rowView.findViewById(R.id.memodate); 
      holder.textView4 = (TextView) rowView 
        .findViewById(R.id.memofeedback); 
      holder.reply = (Button) rowView.findViewById(R.id.buttonFeedback); 
      holder.accept = (Button) rowView.findViewById(R.id.buttonAccepteer); 
      rowView.setTag(holder); 
     } else { 
      holder = (ViewHolder) rowView.getTag(); 
     } 

     holder.textView.setText(commentarray.get(position)); 
     holder.textView2.setText(datearray.get(position)); 
     holder.textView3.setText(fromarray.get(position)); 
     holder.textView4.setText(feedback.get(position)); 

     if (holder.textView4.getText().toString().equals("") 
       || holder.textView4.getText().toString().equals(" ")) { 
      holder.textView4.setText(" - "); 
     } 

     // ONCLICKLISTENER FOR REPLYIMAGE 
     holder.reply.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       dialog = new Dialog(NostradamusActivity2.parentcontext); 
       dialog.setContentView(R.layout.memo_dialog); 
       dialog.setTitle("Feedback"); 
       feedbacktxt = (EditText) dialog.findViewById(R.id.memoeddittxt); 

       Button cancel = (Button) dialog 
         .findViewById(R.id.memobtncancel); 

       // CANCEL BUTTON ONCLICKLISTENER REPLYIMAGE 
       cancel.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
         dialog.dismiss(); 
        } 
       }); 

       Button okay = (Button) dialog.findViewById(R.id.memobtnokay); 

       // OKAY BUTTON ONCLICKLISTENER REPLYIMAGE 
       okay.setOnClickListener(new OnClickListener() { 
        private String errormessage; 
        private AlertDialog alertDialog; 

        public void onClick(View v) { 
         Database2 db = new Database2(
           "https://" 
             + website 
             + "/index2.php?option=com_webservices&controller=json&method=core.memos.memo_feedback&token=", 
           "token", token, "id", ids.get(position), 
           "memo_feedback", feedbacktxt.getText() 
             .toString()); 
     } 

     // //ACCEPTEER MEMO BUTTON ONCLICKLISTENER 
     holder.accept.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       AlertDialog.Builder builder = new AlertDialog.Builder(
         NostradamusActivity2.parentcontext); 
       builder.setMessage("Heeft u de opdracht uitgevoerd?") 
         .setCancelable(false) 
         .setPositiveButton("Ja", 
           new DialogInterface.OnClickListener() { 
            private AlertDialog alertDialog; 

            public void onClick(DialogInterface dialog, 
              int id) { 
             Database2 dbaccept = new Database2(
               "http://intern.koeckers.nl/index2.php?option=com_webservices&controller=json&method=core.memos.memo_state&token=", 
               "token", token, "id", ids 
                 .get(position), 
               "memo_completed", "1"); 

                 dialog.dismiss(); 
              } 
             } catch (Exception e) { 
              e.printStackTrace(); 
             } 
            } 
           }) 
         .setNegativeButton("Nee", 
           new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, 
              int id) { 
             dialog.cancel(); 
            } 
           }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 
      } 
     }); 
     return rowView; 
    } 
} 

回答

0

您正在通過ids.get(position)得到該項目。 您正在使用arrayList,而不是使用getTag()方法。 rowView.getTag()會給你從ViewHolder,你可以得到正確的ID和反饋文本。

Database2 dbaccept = new Database2(
    "http://intern.koeckers.nl/index2.php? option=com_webservices&controller=json&method=core.memos.memo_state&token=", 
    "token", token, "id", ids.get(position), 
    "memo_completed", "1"); 
+0

感謝您的回覆。我想獲得物品的絕對位置。當我使用你的方法rowView.getTag()我找回一個對象。我與此有什麼關係。轉換爲字符串而不是整數?我真的不明白...... – nostradamus

+0

不,我以錯誤的方式回答了這個問題。 rowView.getTag()將爲您提供viewHolder對象,該對象將具有行中的視圖。我以爲你從這一行取值。所以你可以做的是添加一個不可見的文本視圖(到ViewHolder),你可以在其中設置位置並在需要的時候使用它。這不是最好的方式,但直到你得到答案你可以使用這種解決方法。 – SurRam

相關問題