在列表视图中点击多activitys [英] Multiple activitys on listview clickable
问题描述
我有一个问题,我的code IV一直试图解决这个错误,但仍然没有运气。我想一个新的活动启动时,在列表视图项被点击我找了一段时间发现这个code
公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
开关(位置)
{
情况下0:意向newActivity =新意图(这一点,superleague.class);
startActivity(newActivity);
打破;
案例1:意向newActivity =新意图(这一点,youtube.class);
startActivity(newActivity);
打破;
案例2:意向newActivity =新意图(这一点,olympiakos.class);
startActivity(newActivity);
打破;
案例3:意向newActivity =新意图(这一点,karaiskaki.class);
startActivity(newActivity);
打破;
案例4:意向newActivity =新意图(这一点,reservetickets.class);
startActivity(newActivity);
打破;
}
}
现在我把这个在我的项目,它的问题所在,但因为我是新来这个公司需要一些时间来解决这个小东西希望你能帮忙吗?
包com.mybasicapp; 进口android.app.Dialog;
进口android.app.ListActivity;
进口android.content.DialogInterface;
进口android.content.DialogInterface.OnCancelListener;
进口android.content.DialogInterface.OnDismissListener;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ImageButton;
进口android.widget.ListView;
进口android.widget.Toast; 公共类菜单扩展ListActivity实现onListItemClickListner {
的ImageButton ImagebuttonOpenDialog; 字符串KEY_TEXTPSS =TEXTPSS;
静态最终诠释CUSTOM_DIALOG_ID = 0; ListView的dialog_ListView; 的String [] =的listContent {
福特,宝马,奥迪,美洲虎,
奔驰,雷诺,ROVER,SAAB,BENTLEY
VOLVO,沃克斯豪尔,SEAT};
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); ListView控件LV = getListView();
lv.setTextFilterEnabled(真); lv.setOnListItemClickListener(新OnListItemClickListener(){
公共无效onListItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
开关(位置)
{
情况下0:意向newActivity =新意图(这一点,
Pickmodel.class);
startActivity(newActivity);
打破;
案例1:意向newActivity =新意图(这一点,
Pickmodel.class);
startActivity(newActivity);
打破;
案例2:意向newActivity =新意图(这一点,
Pickmodel.class);
startActivity(newActivity);
打破;
案例3:意向newActivity =新意图(这一点,
Pickmodel.class);
startActivity(newActivity);
打破;
案例4:意向newActivity =新意图(这一点,
Pickmodel.class);
startActivity(newActivity);
打破;
}
}
ImagebuttonOpenDialog =(的ImageButton)findViewById(R.id.imgbtn1);
ImagebuttonOpenDialog.setOnClickListener(新Button.OnClickListener(){ 公共无效的onClick(查看为arg0){
的ShowDialog(CUSTOM_DIALOG_ID);
}}); } @覆盖
保护对话框onCreateDialog(INT ID){ 对话的对话= NULL; 开关(ID){
案例CUSTOM_DIALOG_ID:
对话框=新的对话框(菜单。该);
dialog.setContentView(R.layout.custom);
dialog.setTitle(自定义对话框); dialog.setCancelable(真);
dialog.setCanceledOnTouchOutside(真); dialog.setOnCancelListener(新OnCancelListener(){ 公共无效onCancel(DialogInterface对话){
// TODO自动生成方法存根
Toast.makeText(菜单。该,
OnCancelListener
Toast.LENGTH_LONG).show();
}}); dialog.setOnDismissListener(新OnDismissListener(){ 公共无效onDismiss(DialogInterface对话){
// TODO自动生成方法存根
Toast.makeText(菜单。该,
OnDismissListener
Toast.LENGTH_LONG).show();
}}); // $ P对话框$ ppare的ListView
dialog_ListView =(ListView控件)dialog.findViewById(R.id.dialoglist);
ArrayAdapter<串GT;适配器
=新ArrayAdapter<串GT;(这一点,
android.R.layout.simple_list_item_1,的listContent);
dialog_ListView.setAdapter(适配器);
dialog_ListView.setOnItemClickListener(新OnItemClickListener(){ 公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
// TODO自动生成方法存根
Toast.makeText(菜单。该,
parent.getItemAtPosition(位置)的ToString()+点击,
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
}}); 打破;
} 返回对话框;
} @覆盖
在prepareDialog(INT ID,对话的对话,包束){保护无效
// TODO自动生成方法存根
super.on prepareDialog(ID,对话,包); 开关(ID){
案例CUSTOM_DIALOG_ID:
//
打破;
} } 公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
长ARG3){
// TODO自动生成方法存根 } }
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
// TODO自动生成方法存根 }
请让我知道如果我失去了一些东西也感谢
P.S我不能运行这个项目,所以我不能打印日志CAT
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / customdialog
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:填充=20dp
安卓了minWidth =300dp>< ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:SRC =@绘制/ popcar/>< ListView控件
机器人:ID =@ + ID / dialoglist
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/><的ImageButton
机器人:ID =@ + ID / imageButton1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:SRC =@绘制/ KJ/>< / LinearLayout中>
由于正在扩展ListActivity时,onItemClickListener将无法工作。
你必须使用onListItemClickListner,然后它会工作。
I have a problem with my code iv been trying to fix this error but still no luck. I would like a new activity to start when a item in the list view is clicked i looked for some time a found this code
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent newActivity = new Intent(this, superleague.class);
startActivity(newActivity);
break;
case 1: Intent newActivity = new Intent(this, youtube.class);
startActivity(newActivity);
break;
case 2: Intent newActivity = new Intent(this, olympiakos.class);
startActivity(newActivity);
break;
case 3: Intent newActivity = new Intent(this, karaiskaki.class);
startActivity(newActivity);
break;
case 4: Intent newActivity = new Intent(this, reservetickets.class);
startActivity(newActivity);
break;
}
}
Now i placed this in my project and it where the problem is but as i am new to this its taking some time to fix the small things hope you can help?
package com.mybasicapp;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
public class menu extends ListActivity implements onListItemClickListner{
ImageButton ImagebuttonOpenDialog;
String KEY_TEXTPSS = "TEXTPSS";
static final int CUSTOM_DIALOG_ID = 0;
ListView dialog_ListView;
String[] listContent = {
"FORD", "BMW", "AUDI", "JAGUAR",
"MERCEDES", "RENAULT", "ROVER", "SAAB", "BENTLEY",
"VOLVO", "VAUXHALL", "SEAT"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnListItemClickListener(new OnListItemClickListener() {
public void onListItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent newActivity = new Intent(this,
Pickmodel.class);
startActivity(newActivity);
break;
case 1: Intent newActivity = new Intent(this,
Pickmodel.class);
startActivity(newActivity);
break;
case 2: Intent newActivity = new Intent(this,
Pickmodel.class);
startActivity(newActivity);
break;
case 3: Intent newActivity = new Intent(this,
Pickmodel.class);
startActivity(newActivity);
break;
case 4: Intent newActivity = new Intent(this,
Pickmodel.class);
startActivity(newActivity);
break;
}
}
ImagebuttonOpenDialog = (ImageButton)findViewById(R.id.imgbtn1);
ImagebuttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
showDialog(CUSTOM_DIALOG_ID);
}});
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(menu.this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Custom Dialog");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setOnCancelListener(new OnCancelListener(){
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
"OnCancelListener",
Toast.LENGTH_LONG).show();
}});
dialog.setOnDismissListener(new OnDismissListener(){
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
"OnDismissListener",
Toast.LENGTH_LONG).show();
}});
//Prepare ListView in dialog
dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
dialog_ListView.setAdapter(adapter);
dialog_ListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
parent.getItemAtPosition(position).toString() + " clicked",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
}});
break;
}
return dialog;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog, bundle);
switch(id) {
case CUSTOM_DIALOG_ID:
//
break;
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
Please let me know if am missing something also thanks p.s I CANT RUN THIS PROJECT SO I CANT PRINT LOG CAT
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customdialog"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:minWidth="300dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/popcar" />
<ListView
android:id="@+id/dialoglist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/kj" />
</LinearLayout>
Since you are extending the ListActivity, the onItemClickListener will not work. You have to use onListItemClickListner and then it will work.
这篇关于在列表视图中点击多activitys的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!