使用ImageButton的Android自定义列表视图

时间:2015-12-18 05:59:20

标签: java android listview imagebutton

enter image description here

有人可以帮我这么做吗?当我点击图像按钮加上(+)显示为0的值编号(textview)变为1,2,3等。这是我的代码

这是ListObat.java     包co.id.zsoft.apotikstore;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class ListObat extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_obat);

        ArrayList<ItemDetails> image_details = GetSearchResults();

        final ListView lv1 = (ListView) findViewById(R.id.listView);
        lv1.setAdapter(new ItemListBaseAdapter(this, image_details));

        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Object o = lv1.getItemAtPosition(position);
                ItemDetails obj_itemDetails = (ItemDetails) o;
                Toast.makeText(ListObat.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
            }
        });
    }

    private ArrayList<ItemDetails> GetSearchResults(){
        ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();

        ItemDetails item_details = new ItemDetails();
        item_details.setName("Pizza");
        item_details.setItemDescription("Spicy Chiken Pizza");
        item_details.setPrice("RS 310.00");
        item_details.setImageNumber(1);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setName("Burger");
        item_details.setItemDescription("Beef Burger");
        item_details.setPrice("RS 350.00");
        item_details.setImageNumber(2);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setName("Pizza");
        item_details.setItemDescription("Chiken Pizza");
        item_details.setPrice("RS 250.00");
        item_details.setImageNumber(3);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setName("Burger");
        item_details.setItemDescription("Chicken Burger");
        item_details.setPrice("RS 350.00");
        item_details.setImageNumber(4);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setName("Burger");
        item_details.setItemDescription("Fish Burger");
        item_details.setPrice("RS 310.00");
        item_details.setImageNumber(5);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setName("Mango");
        item_details.setItemDescription("Mango Juice");
        item_details.setPrice("RS 250.00");
        item_details.setImageNumber(6);
        results.add(item_details);


        return results;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_list_obat, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是ItemDetails.java

package co.id.zsoft.apotikstore;

/**
 * Created by rsr_unitedstate on 12/18/2015.
 */
public class ItemDetails {
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getItemDescription() {
        return itemDescription;
    }
    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public int getImageNumber() {
        return imageNumber;
    }
    public void setImageNumber(int imageNumber) {
        this.imageNumber = imageNumber;
    }

    private String name ;
    private String itemDescription;
    private String price;
    private int imageNumber;
}

这是ItemListBaseAdapter.java

package co.id.zsoft.apotikstore;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * Created by rsr_unitedstate on 12/18/2015.
 */
public class ItemListBaseAdapter extends BaseAdapter {
        private static ArrayList<ItemDetails> itemDetailsrrayList;

        private Integer[] imgid = {
                R.drawable.testimage,
                R.drawable.testimage,
                R.drawable.testimage,
                R.drawable.testimage,
                R.drawable.testimage,
                R.drawable.testimage
        };

        private LayoutInflater l_Inflater;

        public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
            itemDetailsrrayList = results;
            l_Inflater = LayoutInflater.from(context);
        }

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

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = l_Inflater.inflate(R.layout.item_details_view, null);
                holder = new ViewHolder();
                holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
                holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.price);
                holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price);
                holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
                holder.txt_qty = (TextView) convertView.findViewById(R.id.Qty);
                holder.btnkurang = (ImageButton) convertView.findViewById(R.id.btnkurang);
                holder.btntambah = (ImageButton) convertView.findViewById(R.id.btntambah);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
            holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
            holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getPrice());
            holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);
//      imageLoader.DisplayImage("http://192.168.1.28:8082/ANDROID/images/BEVE.jpeg", holder.itemImage);

            return convertView;
        }

        static class ViewHolder {
            TextView txt_itemName;
            TextView txt_itemDescription;
            TextView txt_itemPrice;
            TextView txt_qty;
            ImageView itemImage;
            ImageButton btntambah;
            ImageButton btnkurang;
        }
    }

这是activity_list_obat.xml作为listview布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="co.id.zsoft.apotikstore.ListObat">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/imageButton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:id="@+id/listView"
        android:layout_below="@+id/editText"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

这是item_details_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/photo"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nama Obat"
        android:id="@+id/name"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/photo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Harga Obat"
        android:id="@+id/price"
        android:layout_below="@+id/name"
        android:layout_toEndOf="@+id/photo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nama Apotik"
        android:id="@+id/itemDescription"
        android:layout_below="@+id/price"
        android:layout_toEndOf="@+id/photo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:id="@+id/Qty"
        android:layout_below="@+id/itemDescription"
        android:layout_toEndOf="@+id/photo" />

    <ImageButton
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:id="@+id/btnkurang"
        android:layout_below="@+id/itemDescription"
        android:layout_toEndOf="@+id/Qty"
        android:src="@drawable/minus"/>

    <ImageButton
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:id="@+id/btntambah"
        android:layout_below="@+id/itemDescription"
        android:layout_toEndOf="@+id/btnkurang"
        android:src="@drawable/plus" />

</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

OnClickListener上设置holder.btnkurang,在适配器的holder.btntambah方法中设置getView。 并相应地增加和减少holder.txt_qty的值。

holder.btnkurang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int qty = Integer.parseInt(holder.txt_qty.getText().toString());
                holder.txt_qty.setText(String.valueOf(qty-1));
            }
        });

        holder.btntambah.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int qty = Integer.parseInt(holder.txt_qty.getText().toString());
                holder.txt_qty.setText(String.valueOf(qty+1));
            }
        });

答案 1 :(得分:1)

在自定义适配器中更改这样的getView方法 你还必须做其他一些改变

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = l_Inflater.inflate(R.layout.item_details_view, null);
                holder = new ViewHolder();
                holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
                holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.price);
                holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price);
                holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
                holder.txt_qty = (TextView) convertView.findViewById(R.id.Qty);
                holder.btnkurang = (ImageButton) convertView.findViewById(R.id.btnkurang);
                holder.btntambah = (ImageButton) convertView.findViewById(R.id.btntambah);

                //// add click listener here
                holder.btnkurang.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                        /// change the price
                    }
                });

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
            holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
            holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getPrice());
            holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);

            return convertView;
        }

答案 2 :(得分:1)

只需要一个静态的int变量来记录计数,并在set tag和gettag之后在getview方法中调用ImageButton的clickListener,如下所示:

holder.btnkurang.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        holder.txt_qty.setText(staticIntvariable-1);
    }
});
holder.btntambah.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        holder.txt_qty.setText(staticIntvariable+1);
    }
});

答案 3 :(得分:1)

尝试这种方式对我有用

 mBTIncrement.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int mValue = Integer.parseInt(_value.getText().toString());
                    mValue++;
                    _value.setText("" + mValue);
                }
            });

            _decrease.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    int mValue = Integer.parseInt(_value.getText().toString());
                    mValue--;

                    if (mValue < 0) {
                        System.out.println("not valid");
                    } else
                    {
                        _value.setText( ""+mValue );

                    }
                }
            });