我有一个动态的列表。我用了this tutorial。这一切都正确加载,但是当我滚动时,我的标题出了问题并且无序。我的代码与教程略有不同;左侧的文本,中心而不是图像和两个文本视图。列表视图从Parse.com填充。
这是我的日志猫。
09-26 20:51:27.115 757-757/com.codealchemist.clashmma E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at com.codealchemist.clashmma.SeparatedListAdapter.getView(SeparatedListAdapter.java:105)
at android.widget.AbsListView.obtainView(AbsListView.java:2143)
at android.widget.ListView.makeAndAddView(ListView.java:1831)
at android.widget.ListView.fillUp(ListView.java:708)
at android.widget.ListView.fillGap(ListView.java:647)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3087)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3361)
at android.view.View.dispatchTouchEvent(View.java:7246)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
at android.view.View.dispatchPointerEvent(View.java:7426)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:530)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
... 48 more
LogCat说我的SeparatedListAdapter.java
@Override
public boolean isEnabled(int position) {
return (getItemViewType(position) != TYPE_SECTION_HEADER);
}
public View getView(int position, View convertView, ViewGroup parent)
{
int sectionnum = 0;
for (Object section : this.sections.keySet())
{
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
<----//This is (SeparatedListAdapter.java:105) ----->
if (position == 0) return headers.getView(sectionnum, convertView, parent);
if (position < size) return adapter.getView(position - 1, convertView, parent);
// otherwise jump into next section
position -= size;
sectionnum++;
}
return null;
}
我所遵循的教程没有给出太多解释,以及它的真正作用。那么如何在滚动之前和之后正确显示它?
修改
我填充适配器的地方
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_list);
progressDialog = ProgressDialog.show(fightCard.this, "", "Loading bout...", true);
progressDialog.show();
// Used this tutorial http://custom-listview-with-separate-headers.blogspot.in/
listView = (ListView) findViewById(R.id.Listview);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HomeItem homeItem = (HomeItem) adapter.getItem(position);
Toast.makeText(getBaseContext(), homeItem.getHomeItemLeft().toString() + " " + homeItem.getHomeItemRight().toString(), Toast.LENGTH_LONG).show();
System.out.println("Selected Item : " + homeItem.getHomeItemID());
}
});
PopulateHomePageItemList();
}
private void PopulateHomePageItemList() {
adapter = new SeparatedListAdapter(this);
//create the main event
ParseQuery<ParseObject> MainEventQuery = ParseQuery.getQuery("MainEvent");
MainEventQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if(e == null){
int i = 0;
int size = parseObjects.size();
Log.d("MAIN EVENT SIZE", size + "");
HomeItemList = new ArrayList<HomeItem>();
while (i < size){
HomeItem homeItem = new HomeItem();
homeItem.setHomeItemID(i);
name1 = parseObjects.get(i).getString("red");
homeItem.setHomeItemLeft(name1);
name2 = parseObjects.get(i).getString("blue");
homeItem.setHomeItemRight(name2);
HomeItemList.add(homeItem);
i++;
}
HomeListAdapter = new HomeListAdapter(getApplicationContext(), 0, HomeItemList);
if(HomeListAdapter!= null)
adapter.addSection(" MAIN EVENT ", HomeListAdapter );
}
}
});
//create the co-main event
ParseQuery<ParseObject> CoMain = ParseQuery.getQuery("CoMainEvent");
CoMain.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null){
int i = 0;
int size = parseObjects.size();
Log.d("Co-MAIN SIZE", size + "");
HomeItemList = new ArrayList<HomeItem>();
while (i < size){
HomeItem homeItem = new HomeItem();
homeItem.setHomeItemID(i);
name1 = parseObjects.get(i).getString("red");
homeItem.setHomeItemLeft(name1);
name2 = parseObjects.get(i).getString("blue");
homeItem.setHomeItemRight(name2);
HomeItemList.add(homeItem);
i++;
}
HomeListAdapter = new HomeListAdapter(getApplicationContext(), 0, HomeItemList);
if(HomeListAdapter!= null)
adapter.addSection(" CO-MAIN EVENT ", HomeListAdapter );
}
}
});
//create the fight card
ParseQuery<ParseObject> bout = ParseQuery.getQuery("bout");
bout.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null){
int i = 0;
int size = parseObjects.size();
Log.d("BOUT SIZE", size + "");
HomeItemList = new ArrayList<HomeItem>();
while (i < size) {
HomeItem homeItem = new HomeItem();
homeItem.setHomeItemID(i);
name1 = parseObjects.get(i).getString("red");
homeItem.setHomeItemLeft(name1);
name2 = parseObjects.get(i).getString("blue");
homeItem.setHomeItemRight(name2);
HomeItemList.add(homeItem);
i++;
}
HomeListAdapter = new HomeListAdapter(getApplicationContext(), 0, HomeItemList);
if(HomeListAdapter!= null)
adapter.addSection(" FIGHT CARD ", HomeListAdapter );
}
}
});
listView.setAdapter(adapter);
progressDialog.dismiss();
}
}
答案 0 :(得分:1)
logcat堆栈跟踪表明LinearLayout
正在尝试转换为TextView
。你有没有解决这个错误?