确定。我正在开发一个项目,我已经创建了listview,但是我想点击它并转到另一个页面,例如当你点击一个按钮进入另一个页面时,就像那样,这是我的代码到目前为止:
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class listV extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter( new ArrayAdapter<String>(this, R.layout.listview,Food));
ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
}
});
}
static final String[] Food = new String[]{
"Physical Activity" , "Healthy Diet", "Childhood Obesity"
};
}
任何帮助将不胜感激。 我只是一个初学者,所以请尽量详细解释。
答案 0 :(得分:1)
看起来你已经95%了。在侦听器的onItemClick方法中,您只需像通常那样启动新活动。您可以使用onItemClick的第3个参数来为您提供单击的listview项的位置,并使用它来区分您调用的活动或将其传递给单个活动:
@Override
public void onItemClick(AdapterView<?> arg0, View position, int arg2, long arg3) {
Intent i;
if( position == 1 ){
i = new Intent(listV.this, MyFirstActivity.class);
} else if (position == 2){
i = new Intent(listV.this, MySecondActivity.class);
} else if (position == 3) {
i = new Intent(listV.this, MyThirdActivity.class);
} else {
return;
}
startActivity(i);
}
答案 1 :(得分:1)
我自己是一个相当新的程序员,我正在尝试创造同样的东西。 ListView,用户可以单击列表中的每个项目,每个项目将开始自己的活动。请根据您之前提供的帮助查看我的代码。
package com.tylerbmc.test;
import android.app.ListActivity;
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.ListView;
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.abdominals)));
ListView list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i;
if (position == 1) {
i = new Intent(Main.this, Second.class);
startActivity(i);
}
else if (position == 2) {
i = new Intent(Main.this, Third.class);
startActivity(i);
}
}
});
}
}