开发者

Android ListView problem when refreshing

I've a problem with ListViews. Everytime I try to add an item after it has first been shown, my program FC having following stack frame

Thread [<3> main] (Suspended (exception IllegalStateException))    
ListView.layoutChildren() line: 1603    
AbsListView$CheckForTap.run() line: 1827    
ViewRoot(Handler).handleCallback(Message) line: 587    
ViewRoot(Handler).dispatchMessage(Message) line: 92    
Looper.loop() line: 123    
ActivityThread.main(String[]) line: 4363    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]    
Method.invoke(Object, Object...) line: 521    
ZygoteInit$MethodAndArgsCaller.run() line: 860    
ZygoteInit.main(String[]) line: 618    
NativeStart.main(String[]) line: not available [native method]    

The first time I add items everything works fine but when I add an item to the adapter after it has first been shown and touch the ListView on the Device it crashes.

I use the following Adapter

public class Adapterclass extends BaseAdapter{
//Adapter for Chatview...

private Context con;
private int count = 0;
private List<String>messages;

@Override
public int getCount() {
    return count;
}

@Override
public Object getItem(int position) {
    return messages.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView txt = new TextView(开发者_高级运维con);
    txt.setText(Html.fromHtml(messages.get(position)));
    return txt;
}

public void AddItem(String item){
    messages.add(item);
    count++;

}

Adapterclass(Context con){
    this.con = con;
    messages = new ArrayList<String>();

}

}

(To add an item I call AddItem) Do you have any suggestions? It drives me nuts since many hours =/


Not sure, but you could try adding:

notifyDataSetChanged();

at the end of your AddItem method. This method lets the adapter know that the underlying data has been changed, and the views need to be redrawn.


Extend the ArrayAdapter instead of the BaseAdapter. Then use its add(Object o) function where you're currently using your AddItem() function. It should handle the updating the View for you.


Well, thanks for yours answers.

I tryed changing to a ListAdapter, but this just was the same result.

I also called notifyDataSetChanged but this crashed in the moment I called it my application. But Logcat then told me that it crashed because it is called from the Thread it got not created. Stackframe didn't give a usefull hint also

So, I guess, it also has problems when sending an add to the Adapter that wasn't created from the same Thread, which is a quite ugly and nowhere documented.

So I created a Handler() in the mainclass to do receive messages from Thread and do the task in its own Context and voilá it worked. Many hours lost cause of not getting correct error messages and no hints in the guide ... nice

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜