我创建了一个GridView,我想从每个按钮开始一些活动。 gridView位于一个名为Status的类中,代码如下:
public class Status extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
public static String[] gridViewStrings = {
"Stato",
"Allarmi",
"Impostazioni",
"Numero SIM",
"Dati Impianto",
"Esci",
};
public static int[] gridViewImages = {
R.drawable.chart,
R.drawable.alert,
R.drawable.set,
R.drawable.sim_status,
R.drawable.impianto2,
R.drawable.close,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Intent i = new Intent(getApplicationContext(), Status.class);
i.putExtra("id", position);
startActivity(i);
break;
case 1:
finish();
break;
}
}
});
initInstances();
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("ESA MOBILE App");
}
}
我的Adapter类如下:我想从第一个按钮启动活动并从第二个按钮关闭活动,但是不起作用。
class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
@Override
public int getCount() {
return string.length;
}
@Override
public Object getItem(int p) {
return null;
}
@Override
public long getItemId(int p) {
return 0;
}
@SuppressLint("InflateParams")
@Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = convertView;
}
return grid;
}
}
答案 0 :(得分:1)
我认为你应该更新你的getView()来设置正确的点击监听器。当您在适配器中更新它们时,您似乎还有一个错误:BTW,使用View Holder模式可以提高此代码的效率):
@SuppressLint("InflateParams")
@Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
} else {
grid = convertView;
}
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
// Here you can set your click listener.
// Another option is to have a OnClickListener array (like you have for the strings and images and use it in a similar way).
grid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (p) {
case 0:
// Start activity
break;
case 1:
// Close
break;
default:
// Do something else
break;
}
}
});
return grid;
}
答案 1 :(得分:0)
我不确定,但我认为您要求点击该项目(不在项目内的ImageView上)
所以在获取父级的id之后执行click in adapter,或者您可以在适配器中实现对图像的单击,如:
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast here
}});