我正在尝试通过实现接口而不是扩展 BaseAdapter 子类(如 ArrayAdapter)来实现 ListAdapter。但我得到的只是一个 ClassCastException,它没有告诉我它的具体原因。
首先关于我要开发的应用程序:
我想为支持 Android 的设备创建一个 Hex View(以及后来的 Hex-Editor)。ListAdapter 像往常一样工作并且完美无缺。SectionIndexer 部分却没有。我想让用户滚动浏览大文件,这样他在正常滚动时手指就不会感到疲倦。所以我决定将加载的文件拆分为 1024 字节的部分(我的快速滚动部分)。
所以让我给你看代码:
package in.waslos.kneo.hexviewer.models;
import in.waslos.kneo.hexviewer.R;
import in.waslos.kneo.hexviewer.views.ByteTextView;
import java.util.ArrayList;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;
public class ByteViewModel implements ListAdapter,SectionIndexer {
private ArrayList<DataSetObserver> listener = new ArrayList<DataSetObserver>();
private byte[] byteData; //contains the byte data from some file
private LayoutInflater xmlInflater;
private Context context;
private StringBuffer buffer = new StringBuffer();
private String[] sections;
public ByteViewModel(Context context,byte[] data) {
if(data != null && context != null){
this.byteData = data;
this.context = context;
sections = new String[data.length/1024]; //1K sections
for(int i=0;i<sections.length;i++){
sections[i]=String.format("%d", (i+1));
Log.e("sections",sections[i]);
}
}
}
public class ViewHolder{
TextView address;
ByteTextView binData;
TextView asciiData;
}
private ViewHolder viewHolder;
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public boolean isEnabled(int position) {
return true;
}
@Override
public int getCount() {
return byteData.length%16 == 0 ? byteData.length/16: byteData.length/16 + 1;
}
@Override
public Object getItem(int position) {
return byteData;
}
@Override
public long getItemId(int position) {
return position*16;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
if(xmlInflater==null){
xmlInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
convertView = xmlInflater.inflate(R.layout.hexviewitem, parent,false);
viewHolder = new ViewHolder();
viewHolder.address = (TextView)convertView.findViewById(R.id.address_view);
viewHolder.binData = (ByteTextView)convertView.findViewById(R.id.byte_view);
viewHolder.asciiData= (TextView)convertView.findViewById(R.id.data_view);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolder)convertView.getTag();
//get leading zeros for the address/offset column
viewHolder.address.setText(Long.toString(position*16 + 0x100000000L, 16).substring(1));
//costume view for the byte view column
viewHolder.binData.setByteValue(position*16, byteData);
viewHolder.asciiData.setText(getAsciiRep(position));
return convertView;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
listener.add(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
listener.remove(observer);
}
//gets string ascii representation for a byte
private String getAsciiRep(int pos){
pos = pos*16;
buffer.delete(0, buffer.length());
for(int i = 0;i<16;i++){
if(pos+i<byteData.length)
if(byteData[pos+i]<0x20){
buffer.append('.');
} else {
buffer.append((char)byteData[pos+i]);
}
else {
buffer.append(' ');
}
}
return buffer.toString();
}
@Override
public int getPositionForSection(int section) {
return section*1024;//this function is never called!
}
@Override
public int getSectionForPosition(int position) {
return position/1024;//this function is never called!
}
@Override
public Object[] getSections() {
Log.e("Indexer", "returning sections!"); //this function is never called!
return sections;
}
}
我没有像教程中那样使用哈希映射,因为我只滚动了一个静态数组。所以返回位置应该足够了。这无关紧要,因为这些函数从来没有被调用过。
一切仍然有效,您可以通过正常的滚动滚动,但如果您触摸“抓取器”,应用程序会通知您其强制关闭。我首先认为它仅适用于字符部分字符串,但这也没有解决问题。
然而,这是我得到的堆栈跟踪:
11-04 18:29:13.914: W/dalvikvm(17078): threadid=1: thread exiting with uncaught exception (group=0x4012b560)
11-04 18:29:13.924: E/AndroidRuntime(17078): FATAL EXCEPTION: main
11-04 18:29:13.924: E/AndroidRuntime(17078): java.lang.ClassCastException:in.waslos.kneo.hexviewer.models.ByteViewModel
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.widget.FastScroller.getSectionsFromIndexer(FastScroller.java:291)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.widget.FastScroller.onTouchEvent(FastScroller.java:435)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.widget.AbsListView.onTouchEvent(AbsListView.java:2141)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.widget.ListView.onTouchEvent(ListView.java:3446)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.view.View.dispatchTouchEvent(View.java:3901)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
11-04 18:29:13.924: E/AndroidRuntime(17078): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1737)
11-04 18:29:13.924: E/AndroidRuntime(17078): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1153)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
11-04 18:29:13.924: E/AndroidRuntime(17078): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1721)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2200)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.view.ViewRoot.handleMessage(ViewRoot.java:1884)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.os.Looper.loop(Looper.java:130)
11-04 18:29:13.924: E/AndroidRuntime(17078): at android.app.ActivityThread.main(ActivityThread.java:3835)
11-04 18:29:13.924: E/AndroidRuntime(17078): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 18:29:13.924: E/AndroidRuntime(17078): at java.lang.reflect.Method.invoke(Method.java:507)
11-04 18:29:13.924: E/AndroidRuntime(17078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-04 18:29:13.924: E/AndroidRuntime(17078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-04 18:29:13.924: E/AndroidRuntime(17078): at dalvik.system.NativeStart.main(Native Method)
如果您需要系统信息:我使用运行 Android 2.3.7 (CyanogenMod) 的 HTC Desire GSM。我还尝试了一个强制关闭的模拟环境(Android 2.1)。
我没有弄清楚我做错了什么。Android 没有告诉我比这个堆栈跟踪更多的信息。对此特定查询的 Google 搜索结果为空或“无效”。
我将本教程用于我的 ListAdapter: