我想在列表视图中使用自定义对话框

时间:2016-03-25 02:22:56

标签: android listview customdialog

我正在使用自定义适配器创建列表视图。

在那里,我希望在单击每个项目时显示一个对话框(当然,每个列表中都有一个不同的对话框)。

这是我的代码。

MainActivity:

public class MainActivity extends AppCompatActivity {
    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<MyCustomList> list = new ArrayList<MyCustomList>();
        list.add(new MyCustomList("a", R.string.jangho_string1, R.string.jangho_string2, R.drawable.jangho));
        list.add(new MyCustomList("b", R.string.dae_string1, R.string.dae_string2, R.drawable.dae));
        list.add(new MyCustomList("c", R.string.an_string1, R.string.an_string2, R.drawable.an));

        listView = (ListView)findViewById(R.id.listviewone);

        MyCustomAdapter adapter = new MyCustomAdapter(getApplicationContext(), R.layout.listviewone, list);
        listView.setAdapter(adapter);
    }
}

MyCustomAdapter

public class MyCustomAdapter extends BaseAdapter {

    Context ctx;
    int layout;
    ArrayList<MyCustomList> list;
    LayoutInflater inf;

    public MyCustomAdapter(Context ctx, int layout, ArrayList<MyCustomList> list){
        this.ctx = ctx;
        this.layout = layout;
        this.list = list;

        inf = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public View getView(final int position, View convertView, ViewGroup parent){

        if(convertView == null){
            convertView = inf.inflate(layout, null);
        }

        final View.OnClickListener makeListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)ctx).showDialog(position);
            }
        };

        convertView.setOnClickListener(makeListener);

        return convertView;
    }
    protected Dialog onCreateDialog(int position){
        Dialog dialog = null;

        switch (position){
            case 0:
                break;
        }
        return dialog;
    }
}

我正在使用Android Studio。

3 个答案:

答案 0 :(得分:0)

您可以创建一个显示对话框的方法,并传递单击的listItem中的值

public void showDialog(Context context, String something){
    Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.dialog_layout);

    //Initialize your widgets from layout here

    dialog.show();

   //You can dialog.dismiss() to close the dialog.
}

答案 1 :(得分:0)

首先,使用listview时使用 ViewHolder 。只需使用 listview.setOnItemClickListener

即可

答案 2 :(得分:-1)

尝试此操作以创建自定义对话框

 final Dialog dialog = new Dialog(MainActivity.this);
      // Include dialog.xml file
      dialog.setContentView(R.layout.forgotpassword);
      // Set dialog title
      dialog.setTitle("ALERT!!");
      // set values for custom dialog components - text, image and button
      Button okbtn = (Button) dialog.findViewById(R.id.okbtn);
      Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn);
      final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext);
      dialog.show();
      dialog.getWindow().setSoftInputMode(
              WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
      // if decline button is clicked, close the custom dialog
      cancelbtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              // Close dialog
              dialog.dismiss();
          }
      });
      okbtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String email=emailedittext.getText().toString();
              //do something more here
          }
      });

请参阅此处: - http://coderzpassion.com/android-show-alertdialog/