在notifydatachanged listview适配器中获取excception [英] getting excception in notifydatachanged listview adaptor
问题描述
我在listview上重新填充数据时遇到异常。
以下是代码:
Hi ,
im getting exception on repopulating data on listview.
following is the code :
@Override
public void run(){
// TODO自动生成的方法存根
if(ser.FindClients()。interact.get(a).drive!= null){
//Log.e(.FILEMANAGERACTIVITY,String.valueOf(ser.FindClients()。interact.get(a).drive.size()));
fmd = new FileManagerAdaptor(FileManagerActivity.this,R.layout.activity_item_layout,ser.FindClients()。interact.get(a).drive);
lst。 setAdapter(fmd);
lst.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView View arg1,int arg2,long arg3){
// TODO自动生成的方法存根
// Toast .makeText(this,text,duration)
TextView txt =(TextView)arg1.findViewById(R.id.lblName);
Log.e(FILEMANAGERACTIVITY, 完成);
tmp =(String)txt.getText();
itemPosition = arg2;
new Thread(){
@Override
public void run(){
// TODO自动生成的方法存根
ser.FindClients()。interact.get(a).ListFiles(tmp,ser.FindClients( ).interact.get(a).fileType.get(itemPosition));
//while(ser.FindClients().interact.get(a).working!= false);
// fmd = new FileManagerAdaptor(FileManagerActivity.this,R.layout.activity_item_layout,ser.FindClients()。interact.get(a).fileNames);
// lst .setAdapter(fmd);
//Log.e (\"FILEMANAGERACTIVITY,String.valueOf(ser.FindClients( ).interact.get(a).fileNames.size()));
fmd.Data(ser.FindClients()。interact.get( a).fileNames);
fmd.notifyDataSetChanged();
日志。 e(FILEMANAGERACTIVITY,列出通知数据已更改);
//lst.setAdapter(fmd);
}
} .start();
}
});
}否则
{
Log.e(FILEMANAGERACTIVITY,Dives null);
}
@Override
public void run() {
// TODO Auto-generated method stub
if(ser.FindClients().interact.get(a).drive!=null){
//Log.e("FILEMANAGERACTIVITY", String.valueOf(ser.FindClients().interact.get(a).drive.size()));
fmd = new FileManagerAdaptor(FileManagerActivity.this, R.layout.activity_item_layout, ser.FindClients().interact.get(a).drive);
lst.setAdapter(fmd);
lst.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
//Toast.makeText(this, text, duration)
TextView txt = (TextView)arg1.findViewById(R.id.lblName);
Log.e("FILEMANAGERACTIVITY", "Done");
tmp = (String) txt.getText();
itemPosition = arg2;
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
ser.FindClients().interact.get(a).ListFiles(tmp, ser.FindClients().interact.get(a).fileType.get(itemPosition));
//while(ser.FindClients().interact.get(a).working!=false);
//fmd = new FileManagerAdaptor(FileManagerActivity.this, R.layout.activity_item_layout, ser.FindClients().interact.get(a).fileNames);
//lst.setAdapter(fmd);
//Log.e("FILEMANAGERACTIVITY", String.valueOf(ser.FindClients().interact.get(a).fileNames.size()));
fmd.Data(ser.FindClients().interact.get(a).fileNames);
fmd.notifyDataSetChanged();
Log.e("FILEMANAGERACTIVITY", "List notified data changed");
//lst.setAdapter(fmd);
}
}.start();
}
});
} else
{
Log.e("FILEMANAGERACTIVITY", "Dives null");
}
,而适配器类是这样的:
while the adaptor class is this :
public List< string> data = null;
private Context con = null;
int layoutId = 0;
public FileManagerAdaptor( Context context,int textViewResourceId,List< string> data){
this.data = data;
this.con = context;
this.layoutId = textViewResourceId;
// TODO自动生成的构造函数存根
}
@Override
public int getCount(){
// TODO自动生成方法存根
//返回super.getCount();
返回data.size();
}
@Override
public String getItem(int position){
// TODO自动生成的方法stub
//返回super.getItem(position);
返回data.get(position);
}
public void Data(ArrayList< string> data)
{
this.data = data;
// notifyDataSetChanged();
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
// TODO自动生成方法存根
查看vi = convertView;
LayoutInflater inf =(LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inf.inflate(layoutId,parent,false);
Log.e(FILEMANAGERADAPTOR,data.get(position));
TextView tx =(TextView)vi.findViewById(R。 id.lblName);
tx.setText(data.get(position));
return vi;
}
@Override
public long getItemId(int arg0){
// TODO自动生成方法存根
返回arg0;
}
}
public List<string> data=null;
private Context con = null;
int layoutId=0;
public FileManagerAdaptor(Context context, int textViewResourceId,List<string> data) {
this.data = data;
this.con = context;
this.layoutId = textViewResourceId;
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//return super.getCount();
return data.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
//return super.getItem(position);
return data.get(position);
}
public void Data(ArrayList<string> data)
{
this.data = data;
//notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
LayoutInflater inf =(LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi= inf.inflate(layoutId, parent, false);
Log.e("FILEMANAGERADAPTOR", data.get(position));
TextView tx = (TextView) vi.findViewById(R.id.lblName);
tx.setText(data.get(position));
return vi;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
}
推荐答案
多线程问题......我看到,解决方案在异常中提到了如果。您只能在创建它的线程中修改视图。将此行移出新线程
Multithreading issues... I see, the solution is mentioned in the exception itself. You can only modify the view in the thread you created it. Move this line out of the new thread
fmd.Data(ser.FindClients().interact.get(a).fileNames);
您需要一些并发实用程序来解决此问题,例如Exchanger或Semaphore。尝试通知主线程后台操作已完成并且已返回数据。然后在主线程中设置视图的数据。
这里主线程表示创建视图的线程的线程。
You need some concurrency utilities to solve this, such as Exchanger or Semaphore. Try to notify the main thread that a background operation has finished and data has been returned. Then set the data of the view in the main thread.
Here main thread means the thread that the thread that created the view.
这篇关于在notifydatachanged listview适配器中获取excception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!