gpt4 book ai didi

java - 让ListVIew中的每个项目跳转到其他Activity

转载 作者:行者123 更新时间:2023-12-01 04:57:35 24 4
gpt4 key购买 nike

我在listView布局中有16个项目。如何让每个项目跳转到另一个 Activity 。我已经尝试过这段代码,但没有任何反应:(

这是GroupActivity.java

package com.test.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class GroupActivity extends Activity{
private ListView listView;
private ListViewAdapter listViewAdapter;
private List<Map<String, Object>> listItems;
protected SharedPreferences group_sp;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.group);

listView=(ListView)findViewById(R.id.group1_list);
listItems=getlistItems();
listViewAdapter=new ListViewAdapter(this, listItems);
listView.setAdapter(listViewAdapter);
listView.setTextFilterEnabled(true);

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Log.i("mm", " onItemClick ");
Intent intent=new Intent();
switch(position){
case 0:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 1:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 2:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 3:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 4:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 5:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 6:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 7:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 8:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 9:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 10:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 11:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 12:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 13:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 14:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
case 15:
intent.setClass(getApplicationContext(), ListViewGroup.class);
GroupActivity.this.startActivity(intent);
break;
}

}

});
}

private List<Map<String, Object>> getlistItems() {
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for(int i = 1; i<=16; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("imageButton", R.drawable.goff1);
map.put("textView", "Group"+i);
map.put("imageView",R.drawable.arrow1);
listItems.add(map);
}
return listItems;
}
}

这是来自适配器的代码:

package com.test.listview;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {
private Context context;
private List<Map<String,Object>> listItems; //group list
private LayoutInflater layoutInflater; //View container
protected SharedPreferences group_sp;

/**Custom list control set*/
public final class ListItemView{
public ImageButton imageButton;
public TextView textView;
public ImageView imageView;
}

public ListViewAdapter(Context context,List<Map<String,Object>> listItems){
this.context=context;
layoutInflater=LayoutInflater.from(context);//创建视图容器设置上下文
this.listItems=listItems;
}

public int getCount() {
return listItems.size();
}

public Object getItem(int position) {
return listItems.get(position);
}

public long getItemId(int position) {
return position;
}

/**list settings*/
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView=layoutInflater.inflate(R.layout.group_list, null);
//get space object
final ImageButton imageButton=(ImageButton)convertView.findViewById(R.id.goff1);
imageButton.setBackgroundResource(R.drawable.goff1);
imageButton.setOnClickListener(new View.OnClickListener() {
boolean flag=true;
public void onClick(View v) {
if(flag){
imageButton.setBackgroundResource(R.drawable.gon1);
flag=false;
}else{
imageButton.setBackgroundResource(R.drawable.goff1);
flag=true;
}

}


});
TextView textView=(TextView)convertView.findViewById(R.id.title);
ImageView imageView=(ImageView)convertView.findViewById(R.id.right_image);
// imageView.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View v) {
// Intent intent=new Intent();
// intent.setClass(context, ListViewGroup.class);
// context.startActivity(intent);
// }
// });

//padding image and word
imageButton.setBackgroundResource((Integer)listItems.get(position).get("imageButton"));
textView.setText((String)listItems.get(position).get("textView"));
imageView.setBackgroundResource((Integer)listItems.get(position).get("imageView"));

//addListener(convertView);
return convertView;
}

}

谁能帮我解决这个问题吗?谢谢。

最佳答案

如果您想在 onItemClick 上启动相同的 Activity ,则无需使用switch case 在每一行单击上启动新 Activity ,只需将代码更改为:

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view
, int position,long id) {
Log.i("mm", " onItemClick ");
Intent intent=new Intent(GroupActivity.this,ListViewGroup.class);

GroupActivity.this.startActivity(intent);
}

});

关于java - 让ListVIew中的每个项目跳转到其他Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13855293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com