对话消息不起作用
Dialog message doesn't work
我有这个数组
String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows8", "Max OS X", "Linux", "OS/2" };
我需要显示 ListView 和适配器中的元素,当我单击对话框消息中的某个元素时,我需要显示单击的是什么元素。
public class zadaca1 extends ListActivity {
String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows8", "Max OS X", "Linux", "OS/2" };
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.layout_zadaca1);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values));
ListView listView=getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder= new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Delete entry");
builder.setMessage("Are you sure you want to delete this entry?");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
dialog.cancel();
}
});
// builder.setMessage(((TextView)view).getText());
builder.show();
}
});
}
}
但是当我 运行 在我的 phone 上点击某个项目时它说不幸的是 zadaca1 已经停止。
我也试试
AlertDialog.Builder builder= new AlertDialog.Builder(this);
和
AlertDialog.Builder builder= new AlertDialog.Builder(AlertDialog.Builder.this);
但是这段代码显示错误
您可以通过以下方式获取列表视图中的项目:
TextView nameyouWant = (TextView) view.findViewById(R.id.idofyourItem);
在 ClickListener 中显示警报对话框:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//fetch data inside the listview item
TextView Title = (TextView) view.findViewById(R.id.idofyourItem);
String someTitle = Title.getText().toString();
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
//delete button
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do some stuffs here!
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setTitle("Delete ?");
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
}
});
我试了一下,效果很好
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder= new AlertDialog.Builder(zadaca1.this);
builder.setTitle("You clicked on");
builder.setMessage(((TextView)view).getText());
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
dialog.cancel();
}
});
//
AlertDialog dialog = builder.create();
dialog.show();
我有这个数组
String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows8", "Max OS X", "Linux", "OS/2" };
我需要显示 ListView 和适配器中的元素,当我单击对话框消息中的某个元素时,我需要显示单击的是什么元素。
public class zadaca1 extends ListActivity {
String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows8", "Max OS X", "Linux", "OS/2" };
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.layout_zadaca1);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values));
ListView listView=getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder= new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Delete entry");
builder.setMessage("Are you sure you want to delete this entry?");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
dialog.cancel();
}
});
// builder.setMessage(((TextView)view).getText());
builder.show();
}
});
}
}
但是当我 运行 在我的 phone 上点击某个项目时它说不幸的是 zadaca1 已经停止。
我也试试
AlertDialog.Builder builder= new AlertDialog.Builder(this);
和
AlertDialog.Builder builder= new AlertDialog.Builder(AlertDialog.Builder.this);
但是这段代码显示错误
您可以通过以下方式获取列表视图中的项目:
TextView nameyouWant = (TextView) view.findViewById(R.id.idofyourItem);
在 ClickListener 中显示警报对话框:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//fetch data inside the listview item
TextView Title = (TextView) view.findViewById(R.id.idofyourItem);
String someTitle = Title.getText().toString();
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
//delete button
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do some stuffs here!
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setTitle("Delete ?");
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
}
});
我试了一下,效果很好
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder= new AlertDialog.Builder(zadaca1.this);
builder.setTitle("You clicked on");
builder.setMessage(((TextView)view).getText());
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
dialog.cancel();
}
});
//
AlertDialog dialog = builder.create();
dialog.show();