- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个自定义光标适配器,其中每一行都包含一个复选框、 TextView 和按钮。该按钮最初设置为 GONE,当用户选中我希望按钮显示的复选框,以便用户可以单击它并删除相应的行。
我的问题是,在我的 getView() 中,我无法获得正确的光标,所以不断获得 cursorOutOfBoundsExceptions
。
如果我不再需要它,我还想删除 bindView()。
public class DeleteCampaignCustomCursorAdapater extends CursorAdapter {
protected static final String TAG = null;
DatabaseHelper myDbHelper;
protected SQLiteDatabase db;
private final Activity context;
private LayoutInflater mInflater;
protected ListView mListView;
private Cursor c;
public static int[] checked;
TextView merchantNameView;
Button deleteButton;
CheckBox selectForDelete;
//ImageView moveButton;
public DeleteCampaignCustomCursorAdapater(Context context, Cursor c) {
super(context, c);
this.context = (Activity) context;
mInflater = LayoutInflater.from(context);
this.c = c;
initializeChecked();
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
/*myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class
merchantNameView = (TextView) view.findViewById(R.id.deleteMerchantNameView);
deleteButton = (Button) view.findViewById(R.id.deleteButton);
//selectForDelete = (CheckBox) view.findViewById(R.id.deleteCheckBox);
//moveButton = (ImageView) view.findViewById(R.id.moveButton);
merchantNameView.setText(cursor.getString(cursor.getColumnIndex("merchantName")));
final int rowID = cursor.getInt(cursor.getColumnIndex("_id"));
//final long rowID = mListView.getSelectedItemId();
if (checked[rowID] == 0) {
selectForDelete.setChecked(false);
} else {
selectForDelete.setChecked(true);
}
selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// Make deleteButton appear
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[rowID] = 1;
deleteButton.setVisibility(0);
}
else {
//Hide deleteButton
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[rowID] = 1;
deleteButton.setVisibility(8);
}
}
});
deleteButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(rowID > 0) {
//Log.d(TAG, "Button Click. rowID = "+rowID);
myDbHelper.deleteRecordWithID(rowID);
cursor.requery();
notifyDataSetChanged();
initializeChecked(); // re-set the checked array
}
}
}); */
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class
if (convertView == null) {
mInflater = context.getLayoutInflater();
convertView = mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}
merchantNameView = (TextView) convertView.findViewById(R.id.deleteMerchantNameView);
deleteButton = (Button) convertView.findViewById(R.id.deleteButton);
selectForDelete = (CheckBox) convertView.findViewById(R.id.deleteCheckBox);
merchantNameView.setText(c.getString(c.getColumnIndex("merchantName")));
final int rowID = c.getInt(c.getColumnIndex("_id"));
//final long rowID = mListView.getSelectedItemId();
if (checked[position] == 0) {
selectForDelete.setChecked(false);
} else {
selectForDelete.setChecked(true);
}
selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// Make deleteButton appear
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[position] = 1;
deleteButton.setVisibility(0);
}
else {
//Hide deleteButton
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[position] = 1;
deleteButton.setVisibility(8);
}
}
});
deleteButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(rowID > 0) {
//Log.d(TAG, "Button Click. rowID = "+rowID);
myDbHelper.deleteRecordWithID(rowID);
c.requery();
notifyDataSetChanged();
initializeChecked(); // re-set the checked array
}
}
});
return convertView;
}
public void initializeChecked() {
checked = new int[c.getCount()];
int i = 0;
while (i < c.getCount()) {
checked[i] = 0;
i++;
}
}
日志:
05-12 00:36:23.232: W/KeyCharacterMap(15777): Can't open keycharmap file
05-12 00:36:23.232: W/KeyCharacterMap(15777): Error loading keycharmap file '/system/usr/keychars/synaptics-rmi-touchscreen.kcm.bin'. hw.keyboards.65537.devname='synaptics-rmi-touchscreen'
05-12 00:36:23.232: I/KeyCharacterMap(15777): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-12 00:36:23.332: D/WindowManagerImpl(15777): addView, new view, mViews[1]: com.android.internal.policy.impl.PhoneWindow$DecorView@4053e328
05-12 00:36:23.903: D/View(15777): onTouchEvent: viewFlags: 0x18005001
05-12 00:36:23.903: D/View(15777): onTouchEvent: isFocusable: true, isFocusableInTouchMode: false, isFocused: false; focusTaken: false
05-12 00:36:23.913: D/WindowManagerImpl(15777): finishRemoveViewLocked, mViews[1]: com.android.internal.policy.impl.PhoneWindow$DecorView@4053e328
05-12 00:36:23.973: D/Database(15777): dbopen(): path = /data/data/com.roardog.FidelrBasic/databases/Fidelr_memory, flag = 6, file size = 5120
05-12 00:36:23.973: D/Database(15777): dbopen(): path = /data/data/com.roardog.FidelrBasic/databases/Fidelr_memory, mode: wal, disk free size: 253 M, handle: 0x387968
05-12 00:36:23.983: D/ATRecorder(15777): com.htc.autotest.dlib.RecordEngine in loader dalvik.system.DexClassLoader@40550268
05-12 00:36:23.983: D/WindowManagerImpl(15777): addView, new view, mViews[1]: com.android.internal.policy.impl.PhoneWindow$DecorView@40548d90
05-12 00:36:23.993: D/AndroidRuntime(15777): Shutting down VM
05-12 00:36:23.993: W/dalvikvm(15777): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
05-12 00:36:24.023: E/AndroidRuntime(15777): FATAL EXCEPTION: main
05-12 00:36:24.023: E/AndroidRuntime(15777): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:585)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
05-12 00:36:24.023: E/AndroidRuntime(15777): at com.roardog.FidelrBasic.DeleteCampaignCustomCursorAdapater.getView(DeleteCampaignCustomCursorAdapater.java:125)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.widget.AbsListView.obtainView(AbsListView.java:1449)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.widget.ListView.measureHeightOfChildren(ListView.java:1265)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.widget.ListView.onMeasure(ListView.java:1128)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.view.View.measure(View.java:8553)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.view.View.measure(View.java:8553)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.view.View.measure(View.java:8553)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.view.View.measure(View.java:8553)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.view.ViewRoot.performTraversals(ViewRoot.java:915)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.os.Looper.loop(Looper.java:150)
05-12 00:36:24.023: E/AndroidRuntime(15777): at android.app.ActivityThread.main(ActivityThread.java:4385)
05-12 00:36:24.023: E/AndroidRuntime(15777): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 00:36:24.023: E/AndroidRuntime(15777): at java.lang.reflect.Method.invoke(Method.java:507)
05-12 00:36:24.023: E/AndroidRuntime(15777): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 05-12 00:36:24.023: E/AndroidRuntime(15777): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 05-12 00:36:24.023:E/AndroidRuntime(15777):在 dalvik.system.NativeStart.main( native 方法) 05-12 00:36:25.304: D/进程 (15777): killProcess, pid=15777 05-12 00:36:25.304:D/进程(15777):dalvik.system.VMStack.getThreadStackTrace( native 方法) 05-12 00:36:25.304: D/进程 (15777): java.lang.Thread.getStackTrace(Thread.java:745) 05-12 00:36:25.304: D/进程 (15777): android.os.Process.killProcess(Process.java:797) 05-12 00:36:25.304: D/进程(15777):com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108) 05-12 00:36:25.304: D/进程(15777):java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:854) 05-12 00:36:25.314: D/进程(15777):java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:851) 05-12 00:36:25.314:D/进程(15777):dalvik.system.NativeStart.main( native 方法) 05-12 00:36:25.314: I/Process(15777): 发送信号。 PID:15777 SIG:9
最终工作解决方案
public class DeleteCampaignCustomCursorAdapater extends CursorAdapter {
protected static final String TAG = null;
DatabaseHelper myDbHelper;
protected SQLiteDatabase db;
private final Activity context;
private LayoutInflater mInflater;
protected ListView mListView;
private Cursor c;
public static int[] checked;
TextView merchantNameView;
Button deleteButton;
CheckBox selectForDelete;
//ImageView moveButton;
public DeleteCampaignCustomCursorAdapater(Context context, Cursor c) {
super(context, c);
this.context = (Activity) context;
mInflater = LayoutInflater.from(context);
this.c = c;
c.moveToFirst();
initializeChecked();
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
c.moveToPosition(position);
myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class
if (convertView == null) {
mInflater = context.getLayoutInflater();
convertView = mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}
merchantNameView = (TextView) convertView.findViewById(R.id.deleteMerchantNameView);
deleteButton = (Button) convertView.findViewById(R.id.deleteButton);
selectForDelete = (CheckBox) convertView.findViewById(R.id.deleteCheckBox);
merchantNameView.setText(c.getString(c.getColumnIndex("merchantName")));
final int rowID = c.getInt(c.getColumnIndex("_id"));
if (checked[position] == 0) {
selectForDelete.setChecked(false);
} else {
selectForDelete.setChecked(true);
}
selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// Make deleteButton appear
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[position] = 1;
deleteButton.setVisibility(0);
}
else {
//Hide deleteButton
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
//checked[position] = 1;
deleteButton.setVisibility(8);
}
}
});
deleteButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(rowID > 0) {
//Log.d(TAG, "Button Click. rowID = "+rowID);
myDbHelper.deleteRecordWithID(rowID); //Call deleteRecord on selected row
c.requery(); //requery the cursor
notifyDataSetChanged(); //notify the view the data set has changed
initializeChecked(); // re-set the checked array
}
}
});
return convertView;
}
public void initializeChecked() {
checked = new int[c.getCount()];
int i = 0;
while (i < c.getCount()) {
checked[i] = 0;
i++;
}
}
最佳答案
在 initializeChecked();
之前将 c.moveToFirst();
添加到你的构造函数中。
编辑
首先在您的 getView 中添加:
c.moveToPosition(position);
关于android - 在 CustomCursor Adapter getView() 中获取正确的光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10553389/
我想要自定义微调器,在下拉列表中包含图像和 TextView ,因此我创建了不同的布局并对其进行了膨胀,其工作正常,但对于按微调器之前的微调器布局,我只想有一个类似“选择”的文本来自列表”但是当我用
所以我正在开发实时 ListView ,我需要 ListView 在单击按钮时更新,这些也是如此: private void ReloadData(){ DataList.clear();
我知道其他人也问过这个问题,但我看过其他解决方案,但仍然无法使其发挥作用。 适配器代码: private class CustomTextAndImageAdapter extends ArrayAd
我有一个通过 asynctask 填充的自定义 ListView 适配器,我在 onprogress 函数中调用 notificationdatasetchanged,并且 getCount() 返回
我正在显示联系人列表。一切都很好,除了设备有 1620 个联系人,所以列表滚动非常慢,甚至有时会挂起。 请帮帮我。 我尝试使用检查 ConvertView!=null 的 getView 方法,但它总
我想在每张图片下方添加一个文本,这是我的实际代码: @Override public View getView(int position, View convertView, ViewGroup pa
我在 Adapter extensions override of getView 中经常看到这一点: @Override public View getView(int position,
我正在尝试在对话框中制作 ListView 。但是我的适配器中的 getView 函数永远不会被调用。有什么想法吗? 适配器: public class DialogAdapter extends B
我有 public class MainActivity extends FragmentActivity 我编写了实现 AsyncResponse 的 Loader 类: public class
我有这样一段代码:)我想编辑我的自定义首选项布局的文本属性。但是从 getView 函数对对象所做的任何更改都不会影响首选项屏幕中的实际列表。有任何想法吗?我知道我不能扩展 PreferenceScr
我有一个扩展 simplecursoradapter 的适配器。新 View 应该从数据库中获取光标和图像,用几个复选框填充列表。出于某种原因,我似乎看不到,我的 getView 甚至没有被调用。我在
谁能解释一下这两种 getView() 实现之间的区别是什么? 第一个简单地检查 convertView 是否为 null;如果它为 null,则膨胀一个新的 View 对象。然后给子View设置合适
我做了一个 SingleItemAdapter extends ArrayAdapter,这个定制的 Adapter 用于 ListView。 在这个 SingleItemAdapter 中,我做了一
这里是相当新的 Android 开发者。 我遇到了一个奇怪的问题,我不确定如何解决。我在这里阅读了很多问题,听起来它们与我遇到的问题相同,但他们问题的解决方案似乎永远不适用于这里发生的事情。 我使用扩
我有一个带有 ListFragment 的自定义适配器,但根本没有调用适配器 getView()。 这是适配器的样子 - public class ModuleListItemAdapter exte
我正在将自定义 SimpleCursorAdapter 设置为 ListView。出于某种原因,对于数据库中的每个项目,FriendAdapter 的 getView() 被调用两次。经过一些调查(我
我有一个包含自定义适配器和自定义行的列表。 问题是当我滚动 ListView 时,图像位于错误的位置... 这是自定义行 xml: 这是 GetView: List it
public View getView(int index, View view, ViewGroup parent){ if (view == null) { // for the firs
我添加了两个按钮(添加和删除)来控制 ListView ,当我删除一个项目时,该项目不会立即在 ListView 中消失,只有当我在 ListView 上滑动时,新项目才会消失。除非我触摸屏幕或在 L
我已经多次尝试在我的 Controller 上使用 this.getView(),但我总是在我的控制台上收到 getView is not a function 错误。 是否有任何“技巧”或我可以做的
我是一名优秀的程序员,十分优秀!