gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:58:45 26 4
gpt4 key购买 nike

每当用户点击列表项行时,我都会尝试显示对话框,但每当我点击列表项行时什么也得不到,为什么?

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

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;

}
}

最佳答案

将您的对话框放入您的 Activity 中,您必须在其中为 ListView 单击编写代码,在内部单击显示您的对话框,

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

}
});

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

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

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.
}

});

关于android - 如何在用户点击列表项行时显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17404182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com