如何在onItemClickListener中创建ListView

时间:2015-10-29 20:55:06

标签: android listview

我有一个GridView,当单击一个元素时,会显示其文本,如下所示。

public class NavigationActivity extends AppCompatActivity {

GridView gridView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //code
    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v,
                                int position, long id) {
            Toast.makeText(
                    getApplicationContext(),
                    ((TextView) v.findViewById(R.id.label)).getText(), Toast.LENGTH_SHORT).show();
        }
    });
}

点击某个项目时,我不想显示简单的文字,而是希望该应用能够弹出"弹出"列表,以便用户可以选择我将处理的选项。我甚至不确定ListView是否是我应该寻找的东西。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:0)

Documents. 或者只是示例代码:

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color)
           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
    });
    return builder.create();
}

P.S。在create to immidiate显示对话框之后添加.show();

答案 1 :(得分:0)

您必须实现Dialog Fragment,这是一个带有自定义布局的对话框(弹出窗口)。

一个例子:

public class YourDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_choice_list, null);

    mList = (ListView)v.findViewById(R.id.your_list);

    final MyListAdapter adapter = new MyListAdapter(this,R.layout.list_item, mList);
    mList.setAdapter(adapter);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(getArguments().getInt("title") + "").setView(v);

    return builder.create();
}

答案 2 :(得分:-1)

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    String strName = arrayAdapter.getItem(which);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color)
    builder.setMessage(strName);
           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {

           }
    });
    return builder.create();
}