gpt4 book ai didi

java - 为每个元素添加一个按钮到 ListView

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

我正在创建一个购物车应用程序,我正在尝试填充一个名为 items 的 String[] 产品。每个商品都有一个 ID、名称和价格。我遇到的问题是我正在尝试制作自定义 ListView 但无法让它在我的 Android 设备上运行。每次我使用客户 ListView 时,它都会崩溃。也许我做得不对,或者我还需要创建一个客户适配器?我尝试过这些方法,但都没有成功。这是我所拥有的,我目前正在使用 simple_list_item_check 布局,但我只是实现了它,看看它是否有效,其中确实有效。 android 的简单布局可以,但我的自定义不会。有人能引导我走向正确的方向吗?

CustomerView.activity

    import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.CheckedTextView;


public class CustomerView extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_view);

Product[] items = {
new Product(1, "Milk", 21.50),
new Product(2, "Butter", 15.99),
new Product(3, "Yogurt", 14.90),
new Product(4, "Toothpaste", 7.99),
new Product(5, "Ice Cream", 10.00),

};

final ListAdapter theAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_checked, items);

ListView customerlist = (ListView) findViewById(R.id.customerList);
customerlist.setTextFilterEnabled(true);
customerlist.setAdapter(theAdapter);
customerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView check = (CheckedTextView)view;
check.setChecked(!check.isChecked());
String itemselected = "You Touched " +
String.valueOf(theAdapter.getItem(position));

Toast.makeText(CustomerView.this, itemselected, Toast.LENGTH_SHORT).show();

}
});

}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_customer_view, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

产品.java

    public class Product {

private int id;
private String name;
private double price;
private boolean selected;


public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;

}

@Override
public String toString() {
return this.id + " " + this.name + " [$" + this.price + "]";
}
}

row_layout.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="shoppingcart.cop4331.com.shoppingcart.CustomerView">


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to Cart"
android:gravity="right"
/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:textSize="20sp"/>



</LinearLayout>

最佳答案

换行:

final ListAdapter theAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_checked, items);

至:

final ListAdapter theAdapter = new ArrayAdapter<>(this,
R.layout.row_layout , R.id.textview1, items);

您使用了错误的 ArrayAdapter 构造函数:)如果 Root View 不是 TextView (在您的情况下 Root View 是 LinearLayout),您必须传递 TextView id

关于java - 为每个元素添加一个按钮到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29708159/

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