作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 MainActivity 类
public class MainActivity extends AppCompatActivity {
GridView gridView;
Context context;
ArrayList prgmName;
public static String [] prgmNameList={"GFU","FAB","DRAW","ANIM","ABC","DEF","PHH","XSE","HGY"};
public static int [] prgmImages={R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6,R.drawable.sample_7,R.drawable.sample_0};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Custom Example for GridView ...
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new GridCustomAdapter(this, prgmNameList,prgmImages));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// displayView(0);
/*Intent intent = null;
if (position == 0){
intent = new Intent(view.getContext(), ToolbarExample.class);
}
if (position == 1){
intent = new Intent(view.getContext(), FloatingButtonExample.class);
}
startActivity(intent);*/
switch (position) {
case 0:
Intent i = new Intent(getApplicationContext(), ToolbarExample.class);
i.putExtra("id", position);
startActivity(i);
break;
case 1:
Intent j = new Intent(getApplicationContext(), FloatingButtonExample.class);
j.putExtra("id", position);
startActivity(j);
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
});
}
}
我的适配器类如下:我想从适配器类启动多个 Activity ,但它不工作
public class GridCustomAdapter extends BaseAdapter {
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public GridCustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_grid_items, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Intent intent = new Intent(context, ToolbarExample.class);
context.startActivity(intent);*/
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_SHORT).show();
}
});
return rowView;
}
}
最佳答案
这里的一个小问题是从适配器类中删除 click 方法。请参阅下面的代码更新您的代码。
public class GridCustomAdapter extends BaseAdapter {
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public GridCustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_grid_items, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
return rowView;
}
}
GridView
使用OnItemClickListener
,那时候你不能使用click适配器类或自定义类按钮或 View 中的事件。
关于android - 如何从 GridView 中的多个项目启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37026462/
我是一名优秀的程序员,十分优秀!