3

在每行中单击ListView包含ToggleButtonButton和的自定义项时遇到问题。问题:当我单独单击每个视图时,如何为每个视图调用不同的操作?我的代码:TextViewSpinner

public class CustomListAdapter extends BaseAdapter {

             Context _context;
             private ArrayList<String> _list;  

             public CustomListAdapter(Context context, 
                                         ArrayList<String> listItems) {
              _context= context;
              _list = listItems; 
             }

             public int getCount(){
              return _list.size();
             }

             public long getItemId(int position){
              return position;
             }

             public Object getItem(int position){
              return _list.get(position);
             } 



             public View getView(int position,View convertView,ViewGroup parent)
             {
               ViewContainer container = new ViewContainer();       

               if(convertView == null)
               {  
                   LayoutInflater inflater = LayoutInflater.from(_context);
                   convertView = inflater.inflate(R.layout.elements,parent, false);
                   convertView.setTag(container); 
                   convertView.setClickable(true);

                   container.listIcon = (ToggleButton)convertView.findViewById(R.id.togle_item);
                   container.listText = (TextView)convertView.findViewById(R.id.text_listitem);
                   container.spinner=(Spinner)convertView.findViewById(R.id.spinner1);
                   container.but=(Button)convertView.findViewById(R.id.button1);
                  convertView.setOnClickListener(new OnClickListener() {

                       public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
                       {

                         **How can I do that when I click each view individually, different action is called for each view???**
                       }

                    public void onClick(View v) {

                    }

                    }
                  );
               }
                 container = (ViewContainer) convertView.getTag();
                 ToggleButton myListIcon = container.listIcon;
                 myListIcon.setChecked(true);
                 Button MyButton=container.but;
                 TextView myListText = container.listText;
                 myListText.setText(_list.get(position));
                 Spinner MySpinner=container.spinner;
                 MySpinner.setAdapter(adapter);
                 container = null;
                 return convertView;
              }
              private class ViewContainer
              {
                  private ToggleButton listIcon; 
                  private TextView listText;
                  private Spinner spinner;
                  private Button but;
              }

        }
4

4 回答 4

3

我假设,您在 Listview 上有按钮、文本视图和 ..。并且您需要为列表视图上的按钮编写操作。

这是编码格式:

        .....
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.calllog_row, null);
            button1 = (Button) view.findViewById(R.id.button1);
            button1.setOnClickListener(this);
        }
        else {
            view = convertView;
        }

        Button1 = (Button) view.findViewById(R.id.Button1);
        Button1.setTag(position);   // this is important


        return view;
    }
    public void onClick(View v) {
        Integer position = (Integer) v.getTag();
        switch (v.getId()) {
        case R.id.button1:
            Log.e("click position ", "" + position);
            break;

    }

 ......

如果我的假设是正确的,我的编码将对您有所帮助。

于 2012-05-03T18:33:58.857 回答
0

而不是为您需要执行不同操作的行中OnClickListenerconvertView每个添加侦听器:View

//...
container.listIcon.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {
      //action for the toggle Button
   }
});
container.but.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {
      //action for the simple Button
   }
});
container.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
     public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
       //something was selected on the spinner
    }

    public void onNothingSelected(AdapterView parent) {
      // for interface
    }
});
//...
于 2012-05-03T18:20:28.047 回答
0

您需要修改您的 getView ,就像我在下面显示的代码一样。

@Override
        public View getView(int position, View convertView, ViewGroup arg2) {
            ViewContainer container;

            if(convertView == null)
               {  
                   LayoutInflater inflater = LayoutInflater.from(_context);
                   convertView = inflater.inflate(R.layout.elements,parent, false);
                   container = new ViewContainer(); 
                   //convertView.setClickable(true);
                   container.listIcon = (ToggleButton)convertView.findViewById(R.id.togle_item);
                   container.listText = (TextView)convertView.findViewById(R.id.text_listitem);
                   container.spinner=(Spinner)convertView.findViewById(R.id.spinner1);
                   container.but=(Button)convertView.findViewById(R.id.button1);
                   convertView.setTag(container); 
               }else{
                   container=convertView.getTag();
               }
            //Now for set Click event for your Induavidual item in list you can do like below
            //use postion to set id of each item
            container.but.setId(position);
            container.but.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                              //This will give you position of item 
                             int id=v.getId();

                }
            });

        //same you can do for spinner and textview and ToggleButton ,use appropriate method that you want to use 

            return convertView;
        }
于 2012-05-03T18:20:43.493 回答
0
vh.orderButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // ---Starting new intent
            Intent in = new Intent(activity.getApplicationContext(), ActivityOrder.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ;
            // ---sending product no to next activity
            in.putExtra("product_category", productNo);
            in.putExtra("business_id", businessId); 
            // ---starting new activity and expecting some response back
            activity.getApplicationContext().startActivity(in);

            Toast.makeText(activity, productNo + " " + businessId, Toast.LENGTH_SHORT).show();
        }
    });
于 2014-01-17T01:30:33.060 回答