简体   繁体   English

如何获取ListActivity的光标

[英]How to get cursor of ListActivity

I use the most simpliest way in layout to define listview: 我使用布局中最简单的方法定义listview:

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="true" />

Since I need a checked list I use CheckedTextView in listitem template. 因为我需要一个检查列表我用CheckedTextView在列表项模板。 The user can check as many items in list as he want. 用户可以根据需要检查列表中的任意多个项目。 It's working fine. 一切正常。

I have a ListActivity root view: 我有一个ListActivity根视图:

public abstract class ActivityList extends ListActivity {
 protected ListView listView;
 public abstract void renderListView( String filter );
 @Override
 public void onCreate( Bundle savedInstanceState ) {
  super.onCreate( savedInstanceState );
  setContentView( R.layout.commonlist );
  listView = getListView();
 }
}

It is abstract, because ActivitiList will be the parent for other view classes, like products, clients, etc. The abstract renderlistview() will be implemented by children with SimpleCursorAdapter , eg.: 它是抽象的,因为ActivitiList将是其他视图类(例如产品,客户端等的父类。 抽象的renderlistview()将由具有SimpleCursorAdapter ,例如:

public class ActivityProductList extends ActivityList {
 @Override
 public void onCreate( Bundle savedInstanceState ) {
  super.onCreate( savedInstanceState );
  ...
  renderListView( null );
 }
 @Override
 public void renderListView( String filter ) {
  listView.setChoiceMode( ListView.CHOICE_MODE_MULTIPLE );
  ...
  SimpleCursorAdapter sca = new SimpleCursorAdapter( this, R.layout.productlistitemchecked, mCursor, from, to );
  setListAdapter( sca );
 }
}

The ActivityProductList is working fine. ActivityProductList工作正常。 The user does it all he want, then clicks on the FINISH button defined and implemented in parent class, where the listview is, too. 用户按照自己的意愿进行操作,然后单击在父类中定义和实现的“完成”按钮,列表视图也位于该父类中。 His event handler is as follows: 他的事件处理程序如下:

protected void getCheckedItems() {
 try {
   String className = this.getListAdapter().getClass().getName();
   Log.e("NanCal", className);
   Class c = this.getListAdapter().getClass();
   ListAdapter la = this.getListAdapter();
   SimpleCursorAdapter ca = ( SimpleCursorAdapter )la;
   Cursor cursor = ca.getCursor();
   SparseBooleanArray selectedItems = listView.getCheckedItemPositions();
   for( int i = 0; i < selectedItems.size(); i++ ) {
    int selectedPosition = selectedItems.keyAt( i );
    cursor.moveToPosition( selectedPosition );
    long rowId = ca.getItemId( selectedPosition );
    Log.d( "", "row id: " + rowId );
   }
  } catch( Exception exception ) {
   Log.e( "NanCalc", exception.getMessage() + "::" + exception.toString() );
  }
}

The concept is: get the selected item's ids. 概念是:获取所选项目的ID。

However, in the line what I marked by comment the program crashes with error: 但是,在我用注释标记的行中,程序因错误而崩溃: 蚀截图部分

The this.getListAdapter() gives me a CursorAdapter than the cast will crashes. this.getListAdapter()给了我一个CursorAdapterCursorAdapter会崩溃。

Could somebody help me please? 有人可以帮我吗?

It's hard to say why you are getting the issue without more information. 很难说为什么没有更多信息就会遇到问题。 You could debug your code and put a break point at the commented error line and just see what the adapter is a type of. 您可以调试代码,并在注释的错误行处插入一个断点,然后看看适配器是什么类型。

I would also suggested that since your method name is getCheckedItems(), simply use getListView().getCheckedItemIds() , just make sure you have set your choiceMode to either single or multiple. 我还建议,由于您的方法名称为getCheckedItems (),因此只需使用getListView()。getCheckedItemIds() ,只需确保已将choiceMode设置为单个或多个即可。

Edit: Since your approach doesn't really require a cursor, why not just use the listadapter that is returned? 编辑:由于您的方法实际上并不需要光标,为什么不只使用返回的listadapter? getListAdapter(). getListAdapter()。 getItemId () will return your id. getItemId ()将返回您的ID。

I solved the problem with sword... 我用剑解决了这个问题...

I've tried to access the SimpleCursorAdapter from parent class (ActivityList). 我试图从父类(ActivityList)访问SimpleCursorAdapter。 This way didn't work. 这种方式行不通。 Therefore I§ve changed the base class to abstract and add an abstract method which will return the selected row id's. 因此,我已将基类更改为abstract,并添加了abstract方法,该方法将返回选定的行ID。 The abstract method is implemented in child class (ActivityProductList). 抽象方法在子类(ActivityProductList)中实现。

I know this solution doesn't give mention to the original problem. 我知道此解决方案并未提及原始问题。 But it works... 但这有效...

Thank you all responses. 谢谢大家的答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM