【问题标题】:How to get a ListView's elements in a AlertDialog如何在 AlertDialog 中获取 ListView 的元素
【发布时间】:2016-11-25 02:50:16
【问题描述】:

我对 android 比较陌生,我被这个问题困住了。我创建了一个启动对话框的按钮。 AlertDialog(具体来说)然后以编程方式创建一个 listView 并为其分配一个适配器。现在用户可以点击一些元素,ListView 中的 textView 元素变成红色。然后我存储被点击的元素。现在,当我再次创建对话框时,单击的相同文本变为白色。

所以我的问题是,如何事先访问 listView 的元素,以便更改之前单击的项目的颜色。

这是java代码:

private void populateListView(String[] els) {

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.single_listview_item,R.id.txtitem, els);
        list = new ListView(this);
        list.setAdapter(adapter);


        list.setOnItemClickListener( new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {

                    ViewGroup vg=(ViewGroup)view;
                    TextView txt=(TextView)vg.findViewById(R.id.txtitem);

                    if(!currentList.isSelected(position)) {
                        txt.setBackgroundResource(R.color.redPastel);
                        currentList.select(position);
                    }

                    else{
                        txt.setBackgroundResource(R.color.white);
                        currentList.unselect(position);
                    }

            }

        });

    }

//the listener for the button

    public void showDialogListView(View view){
        String [] selection = {};
        Button buttonUsed = null;

        switch (view.getId()){
            case R.id.cuisineButton:
                selection = cuisines.getArray();
                currentList = cuisines;
                buttonUsed = (Button) findViewById(R.id.cuisineButton);
                break;
            case R.id.mealTimeButton:
                selection = times.getArray();
                currentList = times;
                buttonUsed = (Button) findViewById(R.id.mealTimeButton);
                break;
        }

        populateListView(selection);

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setCancelable(true);
        final Button finalButtonUsed = buttonUsed;
        builder.setPositiveButton("Select",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finalButtonUsed.setText(currentList.getSelectionText());
                    }
                });
        builder.setNegativeButton("Cancel",null);
        builder.setView(list);
        AlertDialog dialog=builder.create();
        dialog.show();

    }

}

这是单元素 XML 的布局:

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

    <TextView
        android:id="@+id/txtitem"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textSize="25sp"
        android:gravity="center"
        />
</LinearLayout>

【问题讨论】:

标签: android


【解决方案1】:

你可以像这样得到listView项

lv_view_task.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1,final int arg2,
        long arg3) {
    final View selectedView v = arg1 ; // Save selected view in final variable**

    AlertDialog.Builder alert=new AlertDialog.Builder(ViewTask.this);
    alert.setTitle("title stackoverflow");
    alert.setIcon(R.drawable.ic_launcher);

    alert.setPositiveButton("Fix",new OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            String str=lv_view_task.getItemAtPosition(arg2).toString();
            Toast.makeText(getApplicationContext(), str,Toast.LENGTH_LONG).show();
            //Access view object v here
            TextView label=(TextView) v.findViewById(R.id.label);
            String xyz = label.getText();

        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2011-12-11
    相关资源
    最近更新 更多