可能重复:
list new list activty
当我按下列表的任何一行时,我想开始新的不同的激活。谁能给我一个如何实现这个目标的例子?
答案 0 :(得分:0)
这样的事情:
public class MainAcitvity {
ListView mList = null;
public void onCreate() {
// load list view
mList = (ListView) findViewById(....);
// set whatever data and adapter..
mList.setAdapter(...);
// set a item click
mList.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
Intent i = new Intent(MainActivity.this, DifferentActivity.class) // set your Different activity class here
MainActivity.startActivity(i);
}
});
}
}
答案 1 :(得分:0)
如果您有多项不同的活动,请务必确定要开始的活动。
我的例子是假设列表视图顺序没有改变。
Context mContext = this;
...
public void onItemClick( AdapterView<?> parent, View view, int position, long id) {
if(id == 0) {
mContext.startActivity(new Intent(Main.this,Activity0.class));
} else if (id == 1) {
mContext.startActivity(new Intent(Main.this,Activity1.class));
}
}
每个不同的活动等等。
请注意,您只需要一行来启动活动,节省类型时间和少量内存。
答案 2 :(得分:0)
您可以使用开关(){}来实现。
答案 3 :(得分:0)
您可以将xjaphx代码用于listview。如果您使用自定义适配器作为列表视图,则可以使用以下代码..
listview.setAdapter(new customeAdapter(this));
CustomAdapter的类如下
class CustomAdapter extends BaseAdapter{
private Context context;
CustomAdapter(Context context)
{
this.context=context;
}
@Override
public int getCount() {
return 5;//size of listview
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg1) {
// TODO Auto-generated method stub
return arg1;
}
@Override
public View getView(final int position, View view, ViewGroup viewgroup) {
ViewContainer holder = null;
LayoutInflater inflater = LayoutInflater.from(context);
if (view == null)
{
view = inflater.inflate(R.layout.myfeedlist, null);
holder = new ViewContainer();
holder.feedTitle = (TextView) view.findViewById(R.id.title);
holder.arrow=(ImageView)view.findViewById(R.id.arrow_imageview);
view.setTag(holder);
}
else
{
holder = (ViewContainer) view.getTag();
}
holder.feedTitle.setText(mylist.get(position));
holder.feedTitle.setTextColor(Color.YELLOW);
holder.arrow.setImageResource(R.drawable.arrow);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent=new Intent(currentclass.this,DifferentActivity.class);
startActivity(intent);
finish();
}
});
return view;
}
}
class ViewContainer
{
TextView feedTitle;
ImageView arrow;
}