2017-07-27 7 views
-3

誰でも私の手助けをすることができますどのようにListViewの中にダイアログを追加する。私はcustomDialogの内部にListViewを追加しようとしていますが、エラーが発生しています。ListViewが表示されません。Android - ListView inside dialog

+0

どのようなエラーが発生しますか?関連するコードを追加してください。 – Abhi

答えて

0

現在のダイアログのドキュメントを見つけることができます。

https://developer.android.com/guide/topics/ui/dialogs.html

を使用できるダイアログのリストについては:あなたがにカスタムレイアウトを追加するためにDialogFragmentを使用することができます

@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(); 
} 
3

ダイアログ。


あなたのフラグメントはDialogFragment(app.v4 1)クラス拡張する必要があります:次に、あなたがそれを設定し、ダイアログ内の何かを持つことができます

BlankFragment.java

public class BlankFragment extends DialogFragment { 

    private ListView listView; 

    public BlankFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_blank, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     listView = view.findViewById(R.id.f_blank_list); 

     List<String> strings = Arrays.asList("Hello", "World", "Bye"); 
     ArrayAdapter<String> arrayAdapter 
       = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, strings); 

     listView.setAdapter(arrayAdapter); 
    } 
} 

をレイアウトとしてのアップ:

fragment_blank.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="10dp" 
    tools:context="com.example.myapplication.BlankFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_blank_fragment" /> 

    <ListView 
     android:id="@+id/f_blank_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    </ListView> 
</LinearLayout> 

最後に、あなたはMainActivityからの取引でそれを表示する必要があります:

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private static final String BLANK_FRAGMENT_TAG = "FRAGMENT_TAG"; 

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

    public void loadFragment(View view) { 
     BlankFragment blankFragment = new BlankFragment(); 
     blankFragment.show(getSupportFragmentManager(), BLANK_FRAGMENT_TAG); 
    } 
} 

これが結果です:

DialogFragment with list