2012-07-27 140 views
1

我的listView有問題。我使用自定義適配器類來顯示列表項目。 我的問題是,當我點擊位置1上的列表視圖按鈕時,位置10上的按鈕也被點擊。我如何克服這個問題?當我們在位置1點擊ListView按鈕時,第10個位置的按鈕也被點擊

這裏是我的代碼:

public static class Clockin_Group extends BaseAdapter implements Filterable { 

    private LayoutInflater mInflater; 
    private Context context; 


    public Clockin_Group(Context context) { 
     mInflater = LayoutInflater.from(context); 
      this.context = context; 
    } 
    @Override 
    public int getCount() {   

     if(employeeList==null){ 
       return 0; 
      } 
      else{ 
       return employeeList.length; 
      } 

    } 
    @Override 
    public Object getItem(int position) {   
     return employeeList[position]; 
    } 
    @Override 
    public long getItemId(int position) {   
     return 0; 
    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder;  

     if (convertView == null){ 
      convertView = mInflater.inflate(R.layout.group_clkin_row, null); 
      holder = new ViewHolder(); 
      convertView.setTag(holder); 

     } 

     else{ 
      holder = (ViewHolder) convertView.getTag(); 
      } 


     holder.tv = (TextView) convertView.findViewById(R.id.group_name); 
      holder.tv.setText(employeeList[position]); 
      holder.tv1 = (TextView) convertView.findViewById(R.id.loc_id_tv); 
      holder.tv1.setText("["+empNo_Array[position]+"]"); 
      holder.check = (ImageView) convertView.findViewById(R.id.checkmark); 
     holder.check.setVisibility(View.GONE); 
      holder.time = (TextView) convertView.findViewById(R.id.time); 
      holder.time.setText("Last Clock " +punchType_array[position]+" "+"at"+" "+punchTime_array[position]);     
      holder.clkin_tv = (TextView) convertView.findViewById(R.id.tv_clockin); 
     holder.clkin_tv.setVisibility(View.GONE); 
      holder.button = (Button) convertView.findViewById(R.id.group_button); 

      holder.button.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        employee_id = Integer.parseInt(emp_idList[position]); 
         emp_selected = employeeList[position]; 
         System.out.println("selected Emp.."+emp_selected); 

        boolean Status = false; 
         String type = "In"; 
         SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss"); 
          Date date=new Date(); 
          String s=sdf.format(date); 
          System.out.println("GMT: "+s); 


         try { 
          Status = sendDetails(corpId, user_name, password,employee_id,location_id_str, task_id_str, "0", type); 
         } catch (ClientProtocolException e) {     
          e.printStackTrace(); 
         } catch (IOException e) {     
          e.printStackTrace(); 
         } catch (JSONException e) {     
          e.printStackTrace(); 
         } 

         if(Status){ 

          holder.button.setVisibility(View.INVISIBLE); 
           holder.clkin_tv.setVisibility(View.VISIBLE); 
           holder.time.setTextColor(Color.parseColor("#088A08")); 
           holder.check.setVisibility(View.VISIBLE); 
           holder.time.setText("Last Clock IN at "+sdf.format(new Date()).toString()); 
           System.out.println("Status.."); 
         }      

         else{ 
          Toast.makeText(context, "Clock In Failed", Toast.LENGTH_SHORT).show(); 
          holder.button.setVisibility(View.VISIBLE); 
           holder.clkin_tv.setVisibility(View.INVISIBLE); 

          } 


       } 
      }); 
      /*convertView.setOnClickListener(new OnClickListener() { 
       //private int pos = position; 

       @Override 
       public void onClick(View v) { 
        //Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();  
       } 
       });*/ 

      convertView.setOnTouchListener(new OnTouchListener() { 

       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
        Toast.makeText(context, employeeList[position]+"["+empNo_Array[position]+"]", Toast.LENGTH_SHORT).show(); 
        return false; 
       } 
      }); 
      // convertView.setTag(holder); 



     return convertView; 
    } 
+0

http://stackoverflow.com/questions/8162835/button-events-in-listview – RobinHood 2012-07-27 06:39:56

+0

你是如何解決這個問題的?標籤? – Sunkas 2013-03-20 08:57:38

+0

無法關注您的代碼,電視,tv1,clkin_tv縮寫使其無法跟隨。 – Siddharth 2014-01-17 08:43:31

回答

1

這裏的問題是,在列表中的每個按鈕都將有相同的ID。 您可以在您的活動中編寫onItemClickListener()來代替在基礎適配器中編寫onClik(),該活動將爲您提供ListView中該產品的位置。

+0

我不認爲這是OP所需要的,他們似乎想要在listview單元格中有一個onClick按鈕,而不是點擊列表視圖項目。 – Tony 2012-07-27 06:41:11

1

我認爲你需要使用標籤。

convertView.setTag(holder); 
您的onClick內

然後得到從那裏標籤和行動

String currentPos = arg0.getTag().toString(); 
Integer.parseInt(currentPos) 

希望是有道理的

1

實際上convertView概念被用來重用視圖和有最初創建了10個視圖,當您滾動列表onclick時,監聽器未被更新。

可以使用this tutorial不同onClickListener分配到每個項目

0

設置與不帶支架的位置鍵的標記。

變化

convertView.setTag(holder); 

convertView.setTag(position); 

CHK ..

+0

我通過使用yur代碼獲取異常... 07-27 12:28:15.798:E/AndroidRuntime(700):java.lang.ClassCastException:java.lang.Integer 07-27 12:28:15.798:E/AndroidRuntime(700):\t at com.android.epay.TestClass1 $ Clockin_Group.getView(TestClass1.java:559) 07-27 12:28:15。798:E/AndroidRuntime(700):\t在android.widget.AbsListView.obtainView(AbsListView.java:1294) 我通過使用yur代碼獲取異常... android.widget.ListView.addViewBelow(ListView.java:2896 ) 07-27 12:28:15.798:E/AndroidRuntime(700):\t在 – Nama 2012-07-27 07:01:35

+0

這將如何幫助? – Siddharth 2014-01-17 08:44:03

2

檢查通過設置的ListView的高度和寬度WRAP_CONTENT或FILL_PARENT。

0

//寫一個函數內部適配器

public void setList(List<Employee> list) { 
    mEmployeeList = list; 
    notifyDataSetChanged(); 
} 

//Create a member variable. 
private List<Employee> mEmployeeList = new ArrayList<Employee>(); 

//Now in getView() 

public View getView(int position, View convertView, ViewGroup parent) { 
    /Use like this 

    ViewHolder holder = (ViewHolder)convertView.getTag(); 
    final Employee employee= mEmployeeList.get(position); 

//使用員工設置的所有變量。

 holder.button.setOnClickListener(new OnClickListener(){ 

     @Override 
    public void onClick(View arg0) { 
    Log.i("Adapter", employee.id); 
     }}); 
} 

//調用SETLIST功能從主類通EmployeeList的,讓我知道結果。

相關問題