android-Button's clickevent of listView using BaseAdapter

后端 未结 5 1694
陌清茗
陌清茗 2021-01-17 02:22

I am using Listview With custom Adapter which contain imageview,textview and 3 button (insert,update,delete)requirement is that custom adapter is call every

相关标签:
5条回答
  • 2021-01-17 03:03
    private Context context;
    
    public ListViewAdapter(Context context, String[] dateValues,String[] creditAmountValues,String[] closingBalanceValues,String[] currentAmountValues) 
    {
        super(context, R.layout.transactionlayout, dateValues);
        this.context = context;
    
    }
    
     @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.transactionlayout, parent, false);
    
    
             ((Button)rowView.findViewById(R.id.transactions_historyButtonID)).setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                Toast.makeText(context, ""+position, 4000).show();
            }
        });
    
        return rowView;
      }
    

    get the use of row view using inflater. this is working. let me know if u have any doubts.

    0 讨论(0)
  • 2021-01-17 03:06

    - In think you have not Registered all the Buttons with the OnClickListener Interface.

    0 讨论(0)
  • 2021-01-17 03:09

    Try this, may be it will solve your problem.

    btnAdd.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Integer index = (Integer) arg0.getTag();
    
            Toast.makeText(context, "ADD", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(context,MaterDeviceFormActivity.class);
            intent.putExtra("button","add");
            intent.putExtra("device_address", strDviceAddess[index]);
            context.startActivity(intent);
        }
    });
    
    btnUpdate.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Integer index = (Integer) arg0.getTag();
    
            Toast.makeText(context, "UPDATE", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(context,MaterDeviceFormActivity.class);
            intent.putExtra("button","update");
            intent.putExtra("device_address", strDviceAddess[index]);
            context.startActivity(intent);
        }
    });
    
    btnDelete.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Integer index = (Integer) arg0.getTag();
    
            Toast.makeText(context, "DELETE", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(context,MaterDeviceFormActivity.class);
            intent.putExtra("button","delete");
            intent.putExtra("device_address", strDviceAddess[index]);
            context.startActivity(intent);
        }
    });
    
    0 讨论(0)
  • 2021-01-17 03:16

    You can use the button listener inside the the adapter but the only item to register it will be the last item drawn (ie the one immediately off the list). You need to tell the button listener which item it has been clicked on. In my case I just passed in the position and used that to load any associated info needed to manipulate the list item.

    0 讨论(0)
  • 2021-01-17 03:20

    set the tag for each view in get view method by setTag()

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
    
            if (convertView == null) {                            
                LayoutInflater inflater = context.getLayoutInflater();
                convertView = inflater.inflate(.....);
            } 
    
            ur_view= (views) convertView.findViewById(R.id.....);
                    ur_view.setTag(position);
    
            ur_view.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //do something
                }
            }); 
    

    It will work

    0 讨论(0)
提交回复
热议问题