如何在列表视图中添加单击侦听器,如果ListView已经在xml中按钮?

时间:2017-06-05 10:24:34

标签: android xml listview

如何在列表视图中添加单击侦听器,如果ListView已经在xml中按下了?我想点击按钮打开新活动。

这是我的代码。 WashDetail.xml

      <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:text="Malaber Hill" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="16dp"
                android:layout_marginEnd="20dp"
                android:text="Get Details"
                android:id="@+id/gardetails"
                android:textColor="#FFFFFF"
                android:textSize="20dp"
                android:textStyle="normal"

                />
 </LinearLayout>

activity_wash_detail

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
       <ListView
         android:layout_width="fill_parent"
         android:id="@+id/ls"
         android:layout_below="@+id/headerlay"
         android:focusable="true"
         android:clickable="true"
         android:duplicateParentState="true"
         android:focusableInTouchMode="true"
         android:layout_height="fill_parent">
        </ListView>
 </LinearLayout>

WashDetailActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wash_detail);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    listView = (ListView) findViewById(ls);
    GetDetails=(Button)findViewById(R.id.gardetails);
    Models = new ArrayList<>();
    Models.add(new WashDetailing(R.drawable.spl, "PP AUTOMATIVES", R.drawable.mahindra, "Authorized"));
    adapter = new WashDetailingAdapter(Models, getApplicationContext());

    listView.setAdapter(adapter);

    listView.setClickable(false);



     gardetails.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) 
         {
            Intent intent= new         Intent(getApplicationContext(),GarageDetailActivity.class);
            startActivity(intent);

        }
    });
}

5 个答案:

答案 0 :(得分:0)

我们这样做......但坦率地说,你的问题对我来说并不清楚。

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
 position, long id) {
        }
    });

答案 1 :(得分:0)

表示你想在listview中点击按钮,对吗?

答案 2 :(得分:0)

在适配器类的getView方法中设置此单击侦听器

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

       ViewHolder holder = null;

        // Inflater for custom layout
        LayoutInflater inflater = (LayoutInflater)dContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.customer_list_layout, parent, false);

           holder.gardetails = convertView.findViewById(R.id.gardetails);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.gardetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 Intent intent= new                   Intent(getApplicationContext(),GarageDetailActivity.class);
            dContext.startActivity(intent)
            }
        });
        return convertView;
    }

这是您的ViewHolder类

私有类ViewHolder {

Button gardetails;

}

答案 3 :(得分:0)

  

使用自定义适配器这样

public class Custom_Adapter_Products extends BaseAdapter {
    Context context;
    Cursor mCursor; 
    static int[] intArr;
    DatabaseAdapter db;
    public ImageLoader imageLoader;

    public Custom_Adapter_Products(Context context, Cursor mCursor){
        this.context = context;
        this.mCursor = mCursor;
        this.intArr = new int[this.mCursor.getCount()];
        imageLoader=new ImageLoader(context);
    }

    @Override
    public int getCount() {     
        return mCursor.getCount();
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {       
        try{            
                ViewHolder holder;
                this.mCursor.moveToPosition(position);
                LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if (convertView == null)
                {
                    convertView = vi.inflate(R.layout.lv_row_for_products, null);
                    holder = createViewHolder(convertView);
                    convertView.setTag(holder);
                    holder.btnMinus.setTag(holder);
                    holder.btnAdd.setTag(holder);
                } 
                else
                {
                    holder = (ViewHolder) convertView.getTag();                
                }           

                holder.btnMinus.setOnClickListener(new OnClickListener() {              
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    try{
                        ViewHolder holder = (ViewHolder) v.getTag();
                        // Do whatever you want
                    }catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            });

                holder.btnAdd.setOnClickListener(new OnClickListener() {                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    try{
                        ViewHolder holder = (ViewHolder) v.getTag();

                    }catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }}
            });

            String strProductName = this.mCursor.getString(this.mCursor.getColumnIndex(DatabaseAdapter.PRODUCT_NAME));
            String strPrice = this.mCursor.getString(this.mCursor.getColumnIndex(DatabaseAdapter.PRICE));           
            String strMOQ = this.mCursor.getString(this.mCursor.getColumnIndex(DatabaseAdapter.MOQ));
            String strProductId = this.mCursor.getString(this.mCursor.getColumnIndex(DatabaseAdapter.PRODUCT_ID));
            String strMrp = this.mCursor.getString(this.mCursor.getColumnIndex(DatabaseAdapter.MRP));
            holder.tvProductName.setText(strProductName);
            holder.tvPrice.setText(context.getString(R.string.Rupee)+" "+strPrice);

        }catch (Exception e){
            // TODO: handle exception
            e.printStackTrace();
        }
        return convertView;
    }   


    private static class ViewHolder
    {
        public TextView tvProductName;
        public TextView tvPrice;
        public TextView tvMRP;
        public TextView tvMOQ;
        public TextView tvPercent;
        public TextView tvQuantity;       
        public ImageView imgProduct;
        public ProgressBar pBar;
        public Button btnMinus;
        public Button btnAdd;       
    }

    private ViewHolder createViewHolder(View v) {
        ViewHolder holder = new ViewHolder();
        try{
            holder.tvProductName = (TextView) v.findViewById(R.id.tvProductName);
            holder.tvPrice = (TextView) v.findViewById(R.id.tvPrice);
            holder.tvMRP = (TextView) v.findViewById(R.id.tvMRP);
            holder.tvMOQ = (TextView) v.findViewById(R.id.tvMOQ);
            holder.tvQuantity = (TextView) v.findViewById(R.id.tvQuantity);
            holder.imgProduct = (ImageView) v.findViewById(R.id.imgProduct);
            holder.pBar = (ProgressBar) v.findViewById(R.id.img_pb);
            holder.btnMinus = (Button) v.findViewById(R.id.btnMinus);
            holder.btnAdd = (Button) v.findViewById(R.id.btnAdd);
            holder.tvPercent = (TextView) v.findViewById(R.id.tvPercent);
        }catch (Exception e) {
        // TODO: handle exception
            e.printStackTrace();
    }
        return holder;
    }
}

答案 4 :(得分:0)

在Listview Adapter类

上使用此代码
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.item_rating_rows, null, true);
    Button btn = (Button) listViewItem.findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("__onclick", "" + view.getId());
        }
    });