我正在创建一个购物车应用程序,我正在尝试填充一个名为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);
}
}
Product.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>
答案 0 :(得分:2)
更改行:
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构造函数:) 如果根视图不是TextView(在您的情况下根视图是LinearLayout),则必须传递TextView id
答案 1 :(得分:1)
您需要实现ProductsAdapter。您正在使用的ArrayAdapter构造函数正在尝试设置仅检查项目布局的列表,这些布局在给定产品的情况下无效。您必须创建一个简单的适配器,它将Product对象中的每个变量映射到row_layout.xml。
您可以在此之后为适配器建模 - https://github.com/ucsunil/receipts/blob/master/app/src/main/java/com/amex/receipts/adapters/ItemsAdapter.java。这是我在某个时候做过的练习项目,它是将项目添加到购物车中。你基本上会覆盖getView()方法来绑定你的视图。
答案 2 :(得分:0)
我会创建一个自定义适配器类,并在该适配器类中有一个回调。您可以修改此代码以获取String []或ArrayList,并使用XML布局。
这个示例代码有点矫枉过正,但这是我现在提供示例所能做的最好的。
public class WorkoutCardsAdapter extends ArrayAdapter<WorkoutView> {
private LayoutInflater mInflater;
String TAG = WorkoutCardsAdapter.class.getCanonicalName();
Callback mCallback;
List<WorkoutView> mViews;
public WorkoutCardsAdapter(Context context, List<WorkoutView> views, Callback callback) {
super(context, 0, views);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mCallback = callback;
this.mViews = views;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
WorkoutView workoutView = getItem(position);
convertView = null;
if (convertView == null) {
holder = new ViewHolder();
if (workoutView.getWorkoutMainCategory().equals("Traditional")) {
if (workoutView.getWorkoutType().equals("Strength")) {
if (workoutView.getWorkoutCategory().equals("Weightlifting")) {
convertView = mInflater.inflate(R.layout.single_workout_layout_traditional_weights, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutCategory = (TextView) convertView.findViewById(R.id.textViewWorkoutCategoryItem);
holder.tvSetsItem = (TextView) convertView.findViewById(R.id.textViewSetsItem);
holder.tvRepsItem = (TextView) convertView.findViewById(R.id.textViewRepsItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvWorkoutWeight = (TextView) convertView.findViewById(R.id.textViewWorkoutWeightItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
convertView.setTag(holder);
} else if (workoutView.getWorkoutCategory().equals("Isometric") || workoutView.getWorkoutCategory().equals("Circuit") ||
workoutView.getWorkoutCategory().equals("Other")) {
convertView = mInflater.inflate(R.layout.single_workout_layout_traditional_strength, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutCategory = (TextView) convertView.findViewById(R.id.textViewWorkoutCategoryItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.textViewWorkoutDescriptionItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
convertView.setTag(holder);
}
} else if (workoutView.getWorkoutType().equals("Endurance")){
if(workoutView.getWorkoutCategory().equals("Running")){
convertView = mInflater.inflate(R.layout.single_workout_layout_traditional_endurance, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutCategory = (TextView) convertView.findViewById(R.id.textViewWorkoutCategoryItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.textViewWorkoutDescriptionItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
} else if (workoutView.getWorkoutCategory().equals("Other")){
convertView = mInflater.inflate(R.layout.single_workout_layout_traditional_endurance, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutCategory = (TextView) convertView.findViewById(R.id.textViewWorkoutCategoryItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.textViewWorkoutDescriptionItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
}
} else if (workoutView.getWorkoutType().equals("Other")){
convertView = mInflater.inflate(R.layout.single_workout_layout_traditional_other, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.textViewWorkoutDescriptionItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
}
} else if (workoutView.getWorkoutMainCategory().equals("CrossFit")) {
//TODO: DEFINE CROSSFIT
if(workoutView.getWorkoutType().equals("For Reps & Time")){
convertView = mInflater.inflate(R.layout.single_workout_layout_crossfit_repstime, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.textViewWorkoutDescriptionItem);
holder.tvSetsItem = (TextView) convertView.findViewById(R.id.textViewSetsItem);
holder.tvRepsItem = (TextView) convertView.findViewById(R.id.textViewRepsItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
} else if (workoutView.getWorkoutType().equals("For Reps")){
convertView = mInflater.inflate(R.layout.single_workout_layout_crossfit_reps, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.textViewWorkoutDescriptionItem);
holder.tvSetsItem = (TextView) convertView.findViewById(R.id.textViewSetsItem);
holder.tvRepsItem = (TextView) convertView.findViewById(R.id.textViewRepsItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
} else if (workoutView.getWorkoutType().equals("For Time")){
convertView = mInflater.inflate(R.layout.single_workout_layout_crossfit_time, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.textViewWorkoutDescriptionItem);
holder.tvSetsItem = (TextView) convertView.findViewById(R.id.textViewSetsItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
} else if (workoutView.getWorkoutType().equals("For Weight")){
convertView = mInflater.inflate(R.layout.single_workout_layout_crossfit_weight, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.textViewWorkoutDescriptionItem);
holder.tvSetsItem = (TextView) convertView.findViewById(R.id.textViewSetsItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
} else if (workoutView.getWorkoutType().equals("Other")){
convertView = mInflater.inflate(R.layout.single_workout_layout_traditional_other, parent, false);
holder.tvWorkoutType = (TextView) convertView.findViewById(R.id.textViewWorkoutTypeItem);
holder.tvWorkoutName = (TextView) convertView.findViewById(R.id.textViewWorkoutName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.textViewWorkoutDescriptionItem);
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
}
}
} else {
holder = (ViewHolder) convertView.getTag();
}
if (workoutView.getWorkoutMainCategory().equals("Traditional")) {
if (workoutView.getWorkoutType().equals("Strength")) {
if (workoutView.getWorkoutCategory().equals("Weightlifting")) {
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvWorkoutCategory.setText(workoutView.getWorkoutCategory());
holder.tvWorkoutWeight.setText(workoutView.getWorkoutWeight());
holder.tvSetsItem.setText(workoutView.getSets());
holder.tvRepsItem.setText(workoutView.getReps());
} else if (workoutView.getWorkoutCategory().equals("Isometric") || workoutView.getWorkoutCategory().equals("Circuit") ||
workoutView.getWorkoutCategory().equals("Other")) {
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvWorkoutCategory.setText(workoutView.getWorkoutCategory());
holder.tvDescription.setText(workoutView.getWorkoutDesc());
}
} else if (workoutView.getWorkoutType().equals("Endurance")){
if(workoutView.getWorkoutCategory().equals("Running")){
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvWorkoutCategory.setText(workoutView.getWorkoutCategory());
holder.tvDescription.setText(workoutView.getWorkoutDistance());
} else if (workoutView.getWorkoutCategory().equals("Other")){
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvWorkoutCategory.setText(workoutView.getWorkoutCategory());
holder.tvDescription.setText(workoutView.getWorkoutDesc());
}
} else if (workoutView.getWorkoutType().equals("Other")){
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvDescription.setText(workoutView.getWorkoutDesc());
}
} else if (workoutView.getWorkoutMainCategory().equals("CrossFit")) {
//TODO: DEFINE CROSSFIT
if(workoutView.getWorkoutType().equals("For Reps & Time")){
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvSetsItem.setText(workoutView.getWorkoutTime());
holder.tvRepsItem.setText(workoutView.getReps());
holder.tvDescription.setText(workoutView.getWorkoutDesc());
} else if (workoutView.getWorkoutType().equals("For Reps")){
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvSetsItem.setText(workoutView.getSets());
holder.tvRepsItem.setText(workoutView.getReps());
holder.tvDescription.setText(workoutView.getWorkoutDesc());
} else if (workoutView.getWorkoutType().equals("For Time")){
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvSetsItem.setText(workoutView.getWorkoutTime());
holder.tvDescription.setText(workoutView.getWorkoutDesc());
} else if (workoutView.getWorkoutType().equals("For Weight")){
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvSetsItem.setText(workoutView.getWorkoutWeight());
holder.tvDescription.setText(workoutView.getWorkoutDesc());
} else if (workoutView.getWorkoutType().equals("Other")){
holder.tvWorkoutName.setText(workoutView.getWorkoutName());
holder.tvWorkoutType.setText(workoutView.getWorkoutType());
holder.tvDescription.setText(workoutView.getWorkoutDesc());
}
}
holder.btnRemoveWorkout = (Button) convertView.findViewById(R.id.btnRemoveWorkout);
holder.btnRemoveWorkout.setTag(position);
holder.btnRemoveWorkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
mCallback.onPressed(position);
}
});
return convertView;
}
@Override
public WorkoutView getItem(final int position) {
return mViews.get(position);
}
@Override
public int getCount() {
return mViews.size();
}
public interface Callback {
void onPressed(int pos);
}
private static class ViewHolder {
public TextView tvDescription;
public TextView tvWorkoutType;
public TextView tvWorkoutCategory;
public TextView tvWorkoutWeight;
public TextView tvSetsItem;
public TextView tvRepsItem;
public TextView tvWorkoutName;
public Button btnRemoveWorkout;
}
}
答案 3 :(得分:0)
您好,请尝试删除支持库import android.support.v7.app.ActionBarActivity;
并扩展Activity而不是ActionBarActivity。我试过了,它工作正常。支持库存在一些问题。