Можно сделать свой .xml-макет и назначить его с помощью builder.setView()
.
В макете создать два ListView
, которые наполнить "половинками" исходного массива имен через адаптеры.
(Напрямую использовать builder.setItems()
не удастся, так как этот метод создает свой собственный ListView
, и добавляет его в макет самостоятельно).
Получится такой результат:

// activity_alert_dialog_custom_view.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
// MyDialogFragment.java
:
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MyDialogFragment extends DialogFragment {
private String[] catNames = {"Васька1", "Рыжик2", "Мурзик3", "Рыжик4", "Мурзик5", "Рыжик6",
"Мурзик7", "Рыжик8", "Мурзик9", "Рыжик10", "Мурзик112"};
private AlertDialog alertDialog;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Выберите кота");
builder.setCancelable(true);
final View customView = getActivity().getLayoutInflater().inflate(
R.layout.activity_alert_dialog_custom_view, null);
builder.setView(customView);
List<String> items1 = new ArrayList<>();
List<String> items2 = new ArrayList<>();
if (catNames != null) {
for (int i = 0; i < catNames.length; i += 2) {
if (i + 2 <= catNames.length) {
// Два кота в строке:
items1.add(catNames[i]);
items2.add(catNames[i + 1]);
} else {
// Остался один кот:
items1.add(catNames[i]);
}
}
}
ListView listView1 = customView.findViewById(R.id.listView1);
ListView listView2 = customView.findViewById(R.id.listView2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1,
items1);
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, // здесь тоже ..._1 !
items2);
listView1.setAdapter(adapter1);
listView2.setAdapter(adapter2);
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
alertDialog.cancel();
Toast.makeText(getActivity(),
"Выбранный кот: "
+ ((ArrayAdapter) ((ListView) parent).getAdapter()).getItem(position),
Toast.LENGTH_SHORT).show();
}
};
listView1.setOnItemClickListener(onItemClickListener);
listView2.setOnItemClickListener(onItemClickListener);
alertDialog = builder.create();
return alertDialog;
}
}
// MainActivity.java
:
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button_onClick(View view) {
MyDialogFragment myDialogFragment = new MyDialogFragment();
FragmentManager manager = getFragmentManager();
myDialogFragment.show(manager, "dialog");
}
}
// activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="button_onClick"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Ссылки: Пока устанавливалась Android Studio и Intel HAXM, успел прочитать введение в программирование на Android
(урок 1,
урок 2)
и про AlertList (урок 30),
а также нагуглить
Android Custom Layout AlertDialog Example,
Android Layout: width half of parent,
Dynamically add elements to a listView Android, How can I set onClickListener on ArrayAdapter?.
И это совсем не JavaFX...