我正在将我的活动更改为我的某个应用程序的片段,并且遇到了以下错误。错误中没有迹象表明导致错误的原因,但我将其缩小到下面的片段。
我还遇到了另一个奇怪的错误,因为当一个项目被添加到列表中时,在剩下活动并再次重新输入之前,不能勾选其复选框?有没有办法让复选框一旦被添加到列表中就可以删除,而不必离开并再次返回?
有人可以向我解释这个错误吗?
片段
import android.app.Dialog;
import android.support.v4.app.ListFragment;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
public class ShoppingList extends ListFragment{
private Cursor cursor;
private static int measurementValue;
private static String unitValue;
private String name;
private String unit;
private int measurement;
private int currMeasurement;
private int newMeasure;
public ShoppingList() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_shopping, container, false);
final DBMain db = new DBMain(getActivity());
db.open();
cursor = db.getAllItems();
String[] values = new String[cursor.getCount()];
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++)
{
String row = cursor.getString(cursor.getColumnIndex("name"));
values[i] = row;
cursor.moveToNext();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.shopping_list_row, values);
setListAdapter(adapter);
FloatingActionButton filterButton = (FloatingActionButton) rootView.findViewById(R.id.addButton);
filterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// custom ingredient_dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.shopping_dialog);
dialog.setTitle("Add Item");
final EditText item = (EditText) dialog.findViewById(R.id.item);
final Spinner measurement = (Spinner) dialog.findViewById(R.id.measurement);
measurement.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
final String measurementValue = measurement.getSelectedItem().toString();
int finalMeasurement = Integer.parseInt(measurementValue);
ShoppingList.measurementValue = finalMeasurement;
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
final Spinner unit = (Spinner) dialog.findViewById(R.id.unit);
unit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String finalUnit = unit.getSelectedItem().toString();
unitValue = finalUnit;
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
Button dialogButtonOk = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom ingredient_dialog
dialogButtonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newItem = item.getText().toString();
db.open();
db.insertItem(newItem, measurementValue, unitValue);
Cursor cursor = db.getAllItems();
//String[] columns = new String[] {db.KEY_NAME, db.KEY_CODE, db.KEY_ROWID};
String[] columns = new String[]{db.KEY_ITEM_NAME, db.KEY_MEASUREMENT, db.KEY_UNIT};
int[] to = new int[]{R.id.ingredientName, R.id.ingredientMeasurement, R.id.ingredientUnit};
final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.shopping_list_row, cursor, columns, to, 0);
//final ListView shoppingList = (ListView) rootView.findViewById(R.id.shoppingList);
//shoppingList.setAdapter(myCursorAdapter);
setListAdapter(myCursorAdapter);
dialog.dismiss();
}
});
Button dialogButtonNext = (Button) dialog.findViewById(R.id.dialogButtonNext);
// if button is clicked, close the custom ingredient_dialog
dialogButtonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newItem = item.getText().toString();
db.open();
db.insertItem(newItem, measurementValue, unitValue);
item.setText("");
Cursor cursor = db.getAllItems();
//String[] columns = new String[] {db.KEY_NAME, db.KEY_CODE, db.KEY_ROWID};
String[] columns = new String[]{db.KEY_ITEM_NAME, db.KEY_MEASUREMENT, db.KEY_UNIT};
int[] to = new int[]{R.id.ingredientName, R.id.ingredientMeasurement, R.id.ingredientUnit};
final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.shopping_list_row, cursor, columns, to, 0);
//final ListView shoppingList = (ListView) rootView.findViewById(R.id.shoppingList);
//shoppingList.setAdapter(myCursorAdapter);
setListAdapter(myCursorAdapter);
}
});
dialog.show();
}
}
);
return rootView;
}
public class ShoppingCustomAdapter extends SimpleCursorAdapter implements CompoundButton.OnCheckedChangeListener {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
//private Context context = this;
//DBMain db = new DBMain(context);
public ShoppingCustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.mContext = context;
this.inflater = LayoutInflater.from(context);
this.cr = c;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
CheckBox checkBox1 = (CheckBox) view.findViewById(R.id.checkBox1);
checkBox1.setOnCheckedChangeListener(this);
int columnIndex = cursor.getColumnIndex("_id");
int columnvalue = cursor.getInt(columnIndex);
checkBox1.setTag(columnvalue);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = (Integer) buttonView.getTag();
final DBMain db = new DBMain(getActivity());
db.open();
//getting information about deleted item from the shopping list
Cursor item = db.getSingleItem(id);
item.moveToPosition(-1);
while(item.moveToNext())
{
name = item.getString(item.getColumnIndex("name"));
measurement = item.getInt(item.getColumnIndex("measurement"));
unit = item.getString(item.getColumnIndex("unit"));
}
item.close();
//check if item is in Kitchen
Cursor check = db.getSingleItem(id);
//if yes, update it
if(check!=null && check.getCount()>0)
{
//get current kitchen measurement
Cursor measure = db.getKitchenMeasurementID(id);
measure.moveToPosition(-1);
while(measure.moveToNext())
{
currMeasurement = measure.getInt(measure.getColumnIndex("measurement"));
}
measure.close();
newMeasure = measurement + currMeasurement;
db.updateContents(name, newMeasure);
}
else
{
db.insertContent(name, measurement, unit);
}
db.deleteItem(id);
}
}
}
片段的XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@android:id/list"
android:layout_below="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/addButton"
app:backgroundTint="@color/floatingButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:clickable="true"
android:src="@drawable/ic_tab_add"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="63dp"
android:layout_marginRight="16dp" />
</android.support.design.widget.CoordinatorLayout>
行布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/ingredientName"
android:layout_width="wrap_content"
android:textColor="#000000"
android:layout_height="wrap_content"
android:padding="5dp"
android:hint="wewewe"/>
<TextView
android:id="@+id/ingredientMeasurement"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ingredientName"
android:padding="5dp"
android:hint="fefefef"/>
<TextView
android:id="@+id/ingredientUnit"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ingredientMeasurement"
android:padding="5dp"
android:hint="qqqqq"/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#fff"
android:text=""/>
</LinearLayout>
Logcat错误
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
答案 0 :(得分:1)
您错误地使用ArrayAdapter
,问题似乎就在这一行:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.shopping_list_row, values);
检查documentation,第二个参数需要布局资源ID,但不是任何资源:
包含要在何时使用的TextView的布局文件的资源ID 实例化视图。
但您的行布局是LinearLayout
。
如果使用更复杂的布局,请使用其他构造函数:
public ArrayAdapter (Context context, int resource,
int textViewResourceId, T[] objects)
您可以使用它的第3个参数来指定布局中的TextView
。
答案 1 :(得分:0)
在代码行
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.shopping_list_row, values);
setListAdapter(adapter);
布局&#34; shopping_list_row&#34;有LinearLayout但数组适配器采用textview,因此你需要设置父级是Textview的布局,或者创建一个带有自定义行的自定义适配器。