2014-02-08 45 views
0
public class ChooseFavorites extends Activity implements OnItemClickListener 
{ 
    StationManager st; 
    MyCustomAdapter arr; 
    ListView list; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_choose_favorites); 
     st = new StationManager(); 
     list = (ListView)findViewById(R.id.stationsList1); 
     arr = new MyCustomAdapter(this, android.R.layout.simple_list_item_multiple_choice, st.getNamesOfStations()); 
     list.setAdapter(arr); 
     list.setOnItemClickListener(this); 


    } 

    class MyCustomAdapter extends ArrayAdapter<String> implements OnClickListener 
    { 

     LayoutInflater inflater; 
     Context ctx; 
     CheckBox cb; 
     String[] stationNames; 
     TextView stationName1; 

     public MyCustomAdapter(Context context, int textViewResourceId, String[] stationNames) { 
      super(context, textViewResourceId, textViewResourceId, stationNames); 
      ctx = context; 
      this.stationNames = stationNames; 
      inflater = LayoutInflater.from(context); 

     } 


     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return 23; 
     } 

     @Override 
     public String getItem(int position) { 
      // TODO Auto-generated method stub 
      return stationNames[position]; 
     } 

     @Override 
     public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
     { 
      // TODO Auto-generated method stub 
      View row = convertView; 
      if(row==null) 
      { // Object reuse 
       inflater = getLayoutInflater(); 
       row = inflater.inflate(R.layout.favorite_row, parent, false); 
      } 
      stationName1 = (TextView)row.findViewById(R.id.textFavoriteItemInRow); 
      stationName1.setText(stationNames[position]); 
      cb=(CheckBox)row.findViewById(R.id.cbCheck); 
      row.setOnClickListener(this); 
      return row; 
     } 


     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT); 
      toast.show(); 


     } 

    } 



    @Override 
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { 
     // TODO Auto-generated method stub 

     Toast toast = Toast.makeText(getApplicationContext(), "" + position, Toast.LENGTH_SHORT); 
     toast.show(); 



    } 

} 

我不知道你能不能看到这只能通过代码... 但如果班上有没有一个onClick我的服装适配器,什么事都不会发生.. 。 了Android不使用“的项目点击收听”点击收听+复选框 - Android电子

但它确实对“点击”方法的工作....

的问题是:我没有位置,我真正需要的...

希望任何人都可以帮助我在!谢谢你们!

+0

试试这个监听onitemclicklistener会给你的列表视图 –

+0

它dosent ...的选定位置,因为在项目点击监听器上一个列表...这不是一个列表... –

+0

当用户点击复选框时,你想检测到? – simekadam

回答

0

您可以使用此事件收到通知的复选框被选中/取消

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 

    } 
} 
+0

它仍然dosnt给我我正在使用哪一行...只有当我将把位置作为最终.... –

+0

有没有问题,使位置final..it真的不应该改变为一个特定的checkox在一段时间,所以随意做出最终.. – simekadam

+0

那么,如果我这样做最后...它给我奇怪的号码... 它只给我0或10 –

0

您可以使用下面:

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
     int position, long id) { 
     Toast.makeText(getApplicationContext(), 
         "Click ListItem Number " + position, Toast.LENGTH_LONG) 
         .show(); 
    } 
}); 

或U可以保存位置标记checkbox和检查checkbox clicklistener

@Override 
     public View getView(int position, View convertView, ViewGroup parent) 
     { 
//   ur code... 

**cb.setTag(position);** 
      row.setOnClickListener(this); 
      return row; 
     } 


     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
**int position = v.getTag();** //cast & check for Tag not null too. 
      Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT); 
      toast.show(); 


     } 
+0

第一个选项,我试过了,它没有工作... 第二个选项并没有那么差:) –

+0

都打瞌睡的工作,CheckBox是在第一个方法和方法的孩子的观点已经给你的位置 –

0

使用复选框setOnCheckedChangeListener e发泄。

尝试像这样...

CheckBox ChkBx = (CheckBox) findViewById(R.id.checkbox); 
ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{ 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
    if (isChecked) 
    { 
     // perform logic 
    } 

    } 
}); 
+0

是啊我知道如何使用它...它仍然dosnt给我我正在使用哪一行... 但tnx无论如何:) –

0
 @Override 
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     // TODO Auto-generated method stub 
     View row = convertView; 
     if(row==null) 
     { // Object reuse 
      inflater = getLayoutInflater(); 
      row = inflater.inflate(R.layout.favorite_row, parent, false); 
     } 
     stationName1 = (TextView)row.findViewById(R.id.textFavoriteItemInRow); 
     stationName1.setText(stationNames[position]); 
     cb=(CheckBox)row.findViewById(R.id.cbCheck); 

     cb.setOnCheckedChangeListener(new CompoundButton. 
       OnCheckedChangeListener(){ 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 
        if (isChecked) 
        { 
         cb.setChecked(isChecked); 
         Toast toast = Toast.makeText(getApplicationContext(), "checked" + position, Toast.LENGTH_SHORT); 
         toast.show(); 
        } 
        else 
        { 
         cb.setChecked(isChecked); 
         Toast toast = Toast.makeText(getApplicationContext(), "unChecked" + position, Toast.LENGTH_SHORT); 
         toast.show(); 
        } 
       } 
      }); 

     return row; 
    } 

} 
+0

这样做的工作吗?它应该..你可以从代码中删除'cb.setChecked(isChecked);'.. – simekadam

+0

以及它dosent ... 如果我删除该行,盒剂量检查在所有(没有V该框) –

+0

看看这个简单的项目..它在那里工作https://github.com/simekadam/checkboxtest – simekadam

0
public View getView(int position, View convertView, ViewGroup parent) { 
    viewHolder holder; 

    // Reuse an existing view, if one is supplied, otherwise create a new one 
    // Check to see if this one is created yet ? 
    if (convertView == null){ 
    // Get inflater 
    LayoutInflater inflater = (LayoutInflater) 
     parentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    // Create a view for it 
    convertView = inflater.inflate(R.layout.test_reprot_lv_items, parent, false); 

    // Creates a holder class to contain all objects of a row 
    holder = new viewHolder(); 
    holder.equipmentID = (TextView) convertView.findViewById(R.id.equipmentID); 
    holder.checkBox  = (CheckBox) convertView.findViewById(R.id.checkBox); 

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    // Set checkBox's Tag with the position of the row in the listview 
    holder.checkBox.setTag(position); 

    // Sets the tag associated with this view for later reuse. 
    convertView.setTag(holder); 

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    holder.checkBox.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      CheckBox c = (CheckBox) v; 
      int location = (Integer) c.getTag(); 

      // Set selected state with the location of the row in the listview 
      itemsList.get(location).setSelectedState(c.isChecked()); 
     } 
    }); 
    } 
    else{ 
    // In order to reuse the preallocated memory, get the holder from View's tag, 
    // which is allocated and saved in this view object. When a view requests 
    // to be rendered and it has not been instantiated, we new a holder and save 
    // it to View's 'Tag' for later reuse. 
    holder = (viewHolder) convertView.getTag(); 

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    // Set checkBox's Tag with the position of the row in the listview 
    holder.checkBox.setTag(position); 

    holder.checkBox.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      CheckBox c = (CheckBox) v; 
      int location = (Integer) c.getTag(); 

      //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
      // Set selected state with the location of the row in the listview 
      // The method setSelectedState, which provided by your own class, 
      // is called to update the checkbox state. 
      itemsList.get(location).setSelectedState(c.isChecked()); 
     } 
    }); 
    } 

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    // Set checkbox state with calling the method getSelectedState which 
    // provied by your own class. 
    holder.checkBox.setChecked(itemsList.get(position).getSelectedState()); 

    return convertView; 
}