如何在AlertDialog中

时间:2016-11-25 02:50:17

标签: android

我对android比较陌生,我坚持这个问题。我创建了一个启动Dialog的按钮。 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>

1 个答案:

答案 0 :(得分:0)

你可以获得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();

        }
    });