簡體   English   中英

如何從我的列表視圖項上的警報對話框(單選)中設置選定的值?

[英]how can i set selected value from alert dialog (single selection) on my list view item?

如何從我的列表視圖項上的警報對話框(單選)中設置選定的值?...我的場景是: - 我有帶有按鈕和文本視圖的列表視圖項 - 單擊我的列表視圖項中的按鈕,打開對話框 - 選擇對話框中單個選擇列表中的值 - 想要在我的列表視圖項內的 textview 中的對話框中顯示此選定值

這是我的代碼: 問題是:通過在對話框中設置在列表項視圖中更改的值,不會在上下滾動時持續存在。

public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        db = new DatabaseHandler(getContext());
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.activity_event_item, parent, false);
            holder = new ViewHolder();
            holder.x = (TextView) convertView.findViewById(R.id.x);
            holder.y = (TextView) convertView.findViewById(R.id.y);
            holder.b = (Button) convertView.findViewById(R.id.b);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String s = x[position];
        if (s != null){
            holder.x.setText("setting text");
        }
        holder.b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                selected = 0;

                builder.setTitle("Select ").setSingleChoiceItems(array1, selected, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        selected = which;
                    }
                })
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        value = array1[selected];
                        holder.y.setText(value + " ");
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });
                builder.create();
                builder.show();
            }

        });

        return convertView;
    }

我用這段代碼實現了同樣的目的

private void showDialoge(Context context, int postion) {
    String[] items = {"Unpaid", " Paid "};

    AlertDialog.Builder builder = new AlertDialog.Builder(context)
            .setSingleChoiceItems(items, postion, null)
            .setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                    int selectedPosition = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
                    // Do something useful withe the position of the selected radio button
                }
            });

    AlertDialog dialog = builder.create();
    dialog.show();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM