如何在用户点击列表项行时显示对话框

时间:2013-07-01 12:09:47

标签: android android-listview

每当用户点击列表项行时我试图显示对话框,但每当我点击列表项行时什么都没有,为什么?

在这里,我只想通过在对话框中接受来自用户的新数量来更新总值,通过在List行中显示该值来代替数量....

CartActivity.java:

public class CartActivity extends Activity {

    ListView mLstView1;
    CartAdapter mViewCartAdpt;

    final private static int DIALOG_QUANTITY = 1;
    EditText update_quantity ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

            mLstView1 = (ListView) findViewById(R.id.listView1);
            mViewCartAdpt = new CartAdapter(CartActivity.this);
            mLstView1.setAdapter(mViewCartAdpt);
            mLstView1.setOnItemClickListener(new OnItemClickListener() {

             @SuppressWarnings("deprecation")
            public void onItemClick(AdapterView<?> parent, View v,
                     final int position, long id) 
             {       
                 showDialog(DIALOG_QUANTITY);
            }
        }); 
    } 

    protected Dialog onCreateDialog(int id) {

        AlertDialog dialogDetails = null;
            switch (id) {
            case DIALOG_QUANTITY:
                LayoutInflater inflater = LayoutInflater.from(this);
                View dialogview = inflater.inflate(R.layout.dialog_layout, null);
                AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
                dialogbuilder.setTitle("Image Information");
                dialogbuilder.setView(dialogview);
                dialogDetails = dialogbuilder.create();
                break;  
            }
            return dialogDetails;
    }

    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
        case DIALOG_QUANTITY:
        final AlertDialog alertDialog = (AlertDialog) dialog;
            Button updateQuantityButton = (Button) alertDialog
                    .findViewById(R.id.btn_login);
            Button cancelbutton = (Button) alertDialog
                    .findViewById(R.id.btn_cancel);
            update_quantity = (EditText) alertDialog
                    .findViewById(R.id.edit_new_qty);                   

            updateQuantityButton.setOnClickListener(new View.OnClickListener() {

                @Override
                    public void onClick(View v) {   

                }

            });

            cancelbutton.setOnClickListener(new View.OnClickListener() {
                @Override                   
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
            });
            break;
        }
    }

    // Called when the activity begins interacting with the user
    @Override
    protected void onResume(){
        super.onResume();
        mViewCartAdpt.notifyDataSetChanged();
    }   
}

CartAdapter.java:

public class CartAdapter extends BaseAdapter {

    public static final String LOG_TAG = "CartAdapter";

    public static final String KEY_TITLE = "title";
    public static final String KEY_COST = "cost";
    public static final String KEY_QTY = "qty";
    public static final String KEY_TOTAL = "total";

    Activity activity;
    LayoutInflater inflater;
    ImageButton mImgBtnDelete;  
    ListView listView;

    private double itemamount = 0;
    private int itemquantity = 0;



    public CartAdapter(Activity a) {
        // TODO Auto-generated constructor stub
        activity = a;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Constants.sItem_Detail.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

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

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.listrow_cart, null);  // listrow_cart

         vi.setClickable(true);
         vi.setFocusable(true);          

         vi.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) 
             {                       

             }       
         }); 

            final TextView title = (TextView) vi.findViewById(R.id.title);
            final TextView qty = (TextView) vi.findViewById(R.id.qty);
            final TextView cost = (TextView) vi.findViewById(R.id.cost);
            final TextView total = (TextView) vi.findViewById(R.id.total);


            HashMap<String, String> item = new HashMap<String, String>();
            item = Constants.sItem_Detail.get(position);

            // Setting all values in listview

            title.setText(item.get(com.example.sample.ItemsActivity.KEY_TITLE));
            cost.setText(item.get(com.example.sample.ItemsActivity.KEY_COST));
            qty.setText("1");

            itemquantity = Integer.parseInt(qty.getText().toString());
            itemamount = Double.parseDouble(cost.getText().toString());
            total.setText(new DecimalFormat("##.#").format(itemamount*itemquantity));

        return vi;

    }   
}

3 个答案:

答案 0 :(得分:1)

单击列表视图的项目后可以使用这种方式然后打开对话框以及列表中的任何更改或更新,然后在列表视图上设置适配器之前使用setNotifyDataChanged()

 lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
          yourActivty.this.showDialog(DIALOG_QUANTITY);          

          }                 
    });

答案 1 :(得分:1)

我认为这是因为onCreateDialog()onPrepareDialog()应该在活动中覆盖,而不是在适配器中。 由于您声明activity属于Activity类型,因此调用activity.showDialog()不会产生任何影响。

您需要做的是将这些方法移动到您希望显示对话框的活动中,然后传递到活动的CardAdapter和实例,并在适配器中使用它。

new CardAdapter(MyActivity.this);
//...
public class CardAdapter .... {
   public MyActivity activity;
   //.......

   activity.showDialog
}

尽管如此,我建议改为使用DialogFragment

答案 2 :(得分:1)

在您的活动中使用对话框,您必须在其中执行listiview点击代码,在内部单击显示您的对话框,

 list.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
         //show dialog.      

          }                 
    });

我可以看到,您的对话框有两个按钮和一个编辑框,无论在编辑框中输入什么值,您都可以使用编辑框值更新列表值。

抓取编辑框值并更新您在适配器上设置的列表数组,以填充列表视图。

updateQuantityButton.setOnClickListener(new View.OnClickListener() {

                @Override
                    public void onClick(View v) {   
                  // here  you have to fetch editbox value and update your arraylist and then notify your adapter.
                }

            });