我需要知道如何在同一活动的对话框中为自定义列表适配器充气,以及如何在添加到Dialog中的自定义列表上实现OnItemClick Listner。
谢谢
答案 0 :(得分:0)
public class MainActivity extends AppCompatActivity implements
View.OnClickListener, AdapterView.OnItemClickListener {
Button button;
String[] data = new String[]{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String day=data[i];
Toast.makeText(this,day,Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View view) {
if(view==button){
View layout = LayoutInflater.from(this).inflate(R.layout.activity_main2, null, false);
listView = (ListView) layout.findViewById(R.id.listView);
ListAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("hello");
builder.setView(layout);
builder.show();
}
}
}