我正在努力学习ListAvtivity
。我编写了一些呈现List的代码,当您单击List中的Item
时,您将进入一个提供一些任意信息的Activity。
我试图想出一些方法,如果用户点击列表中的Item
,他会看到Dialog Box
一些信息,而不是一个全新的Activity
。
来源:
package com.mavenmaverick.listviewtest;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ActivityExample extends ListActivity{
static final String[] CHOICES = new String[]{
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune",
"Pluto",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
getListView().setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case 0:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 1:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 2:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 3:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 4:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 5:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 6:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 7:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 8:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
default:
break;
}
}
});
}
}
答案 0 :(得分:0)
您可以直接在onItemClick
中抓取您的内容。使用parent.getItemAtPosition()
执行此操作。由于您使用字符串数组填充ListView
,getItemAtPosition
将返回一个字符串。
您的代码应如下所示:
getListView().setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String content = (String)parent.getItemAtPosition(position);
// do something with the dialog
showDialog(content);
}
}
你应该更多地浏览ListView。
答案 1 :(得分:0)
您可以在onItemClick
中使用/创建对话框,并将标题设置为planet
的名称,您甚至可以set the view
强>对话到你想要的风格。
<强>样品:强>
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case 0:
Dialog dialog = new Dialog(this);
dialog.setTitle(CHOICES[args]);
dialog.setContentView(R.layout.your_layout_to_dialog); //<-- if you want to add some view to your dialog
dialog.show();
break;
答案 2 :(得分:0)
试试这个家伙会帮助你......
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Youractivity.this);
// Setting Dialog Title
alertDialog.setTitle("AlertDialog");
switch (arg2) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
default:
break;
}
// Setting Dialog Message
alertDialog.setMessage("A Planet in the Solar System"+arg2+"listview Item clicked");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
}