单击按钮时无法更改布局。实际上,我总是使用这段代码:
@Override
public void onClick(View view) {
if (view == imageButtonOrders) {
startActivity(new Intent(this, Order.class)); }
现在,我想在ListView
中使用此功能。但是,当我使用它时,我得到一个错误。我该如何解决?
SwipeListAdapterP.java
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.product_items, null);
TextView pk = (TextView) convertView.findViewById(R.id.pk);
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView point = (TextView) convertView.findViewById(R.id.point);
ImageButton imageButtonDelete = (ImageButton) convertView.findViewById(R.id.imageButtonDelete);
ImageButton imageButtonUpdate = (ImageButton) convertView.findViewById(R.id.imageButtonUpdate);
ProductItems m = productList.get(position);
pk.setText(String.valueOf(m.getPk()));
name.setText(String.valueOf(m.getName()));
point.setText(String.valueOf(m.getPoint()));
imageButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(this, Order.class));
}});
答案 0 :(得分:0)
您需要父活动context
才能从Adapter类启动Activity。
而不是:
startActivity(new Intent(this, Order.class));
使用:
activity.startActivity(new Intent(activity, Order.class));
其中activity
是您传递给Adapter类的Activity
上下文
答案 1 :(得分:0)
以这种方式尝试OnClick
//按findViewById()
@Override
public void onClick(View view) {
if (view.getId() == imageButtonOrders) {
startActivity(new Intent(this, Order.class));
}
适配器中的 您需要使用活动参考初始化Context
并在Context
上传递Intent
imageButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(`Context`, Order.class));
}});