Java 为什么可以';我不能用ListView中的按钮更改布局吗?
单击按钮时无法更改布局。实际上,我总是使用以下代码:Java 为什么可以';我不能用ListView中的按钮更改布局吗?,java,android,listview,Java,Android,Listview,单击按钮时无法更改布局。实际上,我总是使用以下代码: @Override public void onClick(View view) { if (view == imageButtonOrders) { startActivity(new Intent(this, Order.class)); } 现在,我想在我的列表视图中使用这个函数。但是,当我使用它时,我得到了一个错误。我怎样才能修好它 SwipeListAdapterP.java @Ov
@Override
public void onClick(View view) {
if (view == imageButtonOrders) {
startActivity(new Intent(this, Order.class)); }
现在,我想在我的列表视图中使用这个函数。但是,当我使用它时,我得到了一个错误。我怎样才能修好它
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));
}});
您需要父活动上下文才能从适配器类启动活动
而不是:
startActivity(new Intent(this, Order.class));
使用:
其中,activity
是传递给适配器类的activity
上下文
//通过findViewById()强制转换imageButtonOrders
在适配器中,您需要使用活动引用初始化上下文
,并将该上下文
传递到意图
imageButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(`Context`, Order.class));
}});
我尝试了这个,但我从“startActivity”中获取了错误。@alpuktug如果答案有帮助。.单击答案左侧的勾号将其标记为正确。.谢谢提供有关错误的更多信息。
@Override
public void onClick(View view) {
if (view.getId() == imageButtonOrders) {
startActivity(new Intent(this, Order.class));
}
imageButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(`Context`, Order.class));
}});