2013-03-24 46 views
0

標題已經說過了。
我有一個自定義適配器的列表視圖,行只包含一個TextView和一個CheckBox。
我需要確定用戶是否點擊了校驗標記或文本,因此我可以顯示說明對話框,說明他將要檢查的內容!Android ListView確定Text或CheckMark點擊

在我的適配器

我設置一個OnClickListener像這樣:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 

    if(row==null){ 
     LayoutInflater inflater=getLayoutInflater(); 
     row=inflater.inflate(R.layout.entsorgung_list_item, parent, false); 
    } 

    // CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.ctvEntsorgung); 
    // checkedTextView.setText(bedarfsList.get(position)); 

    TextView itemText = (TextView)row.findViewById(R.id.tvEntdorgItem); 
    CheckBox itemCheck = (CheckBox)row.findViewById(R.id.cbEntsorgItem); 

    itemCheck.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Log.d("TRACE","check click pos "); 
      // here i need the ListView position thats been clicked 
     } 
    }); 

    itemText.setText(bedarfsList.get(position)); 

    Boolean checked = myChecked.get(position); 
    if (checked != null) { 
     itemCheck.setChecked(checked); 
    } 

    return row; 
} 

複選框具有可調焦設置爲false。
在我的活動中,我有一個OnItemClickListener,它可以很好地處理文本點擊,我只需要對我的複選框也一樣。

回答

0

但是你已經收到來自getView方法CheckBox的位置,以便使用:

position 

來自:

public View getView(int position, View convertView, ViewGroup parent)

像這樣:

itemCheck.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Log.d("TRACE","check click pos "); 
      Boolean checked = myChecked.get(position); 
     } 
    }); 

編輯:看看下面的適配器我使用了一個非常類似的事例:

private class MySecondAdapter extends ArrayAdapter<MiniTask> 
{ 
    private ArrayList<MiniTask> list; 

    public MySecondAdapter(Context context, int textViewResourceId, ArrayList<MiniTask> miniTaskList) 
    { 
     super(context, textViewResourceId, miniTaskList); 
     this.list = new ArrayList<MiniTask>(); 
     this.list.addAll(miniTaskList); 
    } 
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     miniTask = miniTaskList.get(position); 
     ViewHolder holder = new ViewHolder(); 
     { 
      LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflator.inflate(R.layout.check_list_item_new, null); 

      holder.title = (TextView) convertView.findViewById(R.id.tvItemTitle); 
      holder.commentsPicturesButton = (ImageView) convertView.findViewById(R.id.iAddCommetOrPicture); 
      holder.commentsPicturesButton.setTag(position); 
      holder.commentsPicturesButton.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) 
       { 
        Intent intent = new Intent(getApplicationContext(), PicturesAndCommentsActivity.class); 
        intent.putExtra(TasksListActivity.KEY_ID, task.getId()); 
        intent.putExtra("mini_task_text", miniTask.getTitle()); 
        startActivity(intent); 
       } 
      }); 
      holder.selected = (CheckBox) convertView.findViewById(R.id.cbCheckListItem); 
      holder.selected.setTag(position); 
      holder.selected.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) 
       {  
        {        
         Log.d(TAG, "pressed the checkbox: " + v.getId() + " in position: " + position + " tag: " +v.getTag() +" and item from array: " + miniTaskList.get(position)); 
         CheckBox checkbox = (CheckBox) v; 
         miniTaskList.get(position).setSelected(checkbox.isChecked()); 
         numOfCheckedMiniTasks = 0; 
         for(int i=0;i<miniTaskList.size();i++) 
         { 
          miniTask = miniTaskList.get(i); 
          if(miniTask.isSelected()) 
          { 
           numOfCheckedMiniTasks ++; 
          } 
         } 
         int percent = (int)(numOfCheckedMiniTasks * 100.0f)/miniTaskList.size(); 
         Log.d(TAG, "the percentage is: " +percent); 
         tasksRepository.get(tasksRepository.indexOf(task)).setMiniTasksPercentageComplete(percent); 
        } 
       } 
      }); 

     } 
     holder.title.setText(miniTask.getTitle()); 
     holder.selected.setChecked(miniTask.isSelected()); 
     return convertView; 
    } 
} 

static class ViewHolder 
{ 
    TextView title; 
    CheckBox selected; 
    ImageView commentsPicturesButton; 
} 
+0

顯然我試過了第一,但它說我已經加入 – M4tchB0X3r 2013-03-24 12:39:54

+0

「 變量'position'從whithin腸子類訪問」一個ArrayAdapter的代碼片段,與您的視圖非常相似,請仔細閱讀。也許你會發現你做錯了什麼。 – 2013-03-24 12:42:50

+0

thx。我只是在getView中做了一個最終的int,並給了它一個位置。這我可以在ClickListener中使用!工作正常! – M4tchB0X3r 2013-03-24 12:46:56