gpt4 book ai didi

android - 如何从 GridView 中的多个项目启动 Activity

转载 作者:行者123 更新时间:2023-11-30 01:21:34 25 4
gpt4 key购买 nike

这是我的 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/

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