Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,
im getting exception on repopulating data on listview.
following is the code :

Quote:
@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 :


Quote:
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;
}

}
Posted
Comments
AlphaDeltaTheta 2-Jun-13 21:31pm    
Exception details??
shanalikhan 3-Jun-13 3:53am    
following is the exception getting in logcat

06-03 12:52:21.055: E/AndroidRuntime(10768): FATAL EXCEPTION: Thread-908
06-03 12:52:21.055: E/AndroidRuntime(10768): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-03 12:52:21.055: E/AndroidRuntime(10768): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.


1 solution

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
Java
fmd.Data(ser.FindClients().interact.get(a).fileNames);


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900