當我點擊一個項目在我的列表視圖,我想有一個新的線程啓動並執行我在Connect2類中定義的所需任務,但是,作爲據我所知,線程沒有啓動或顯示爲什麼沒有啓動。如果有人能夠幫助診斷代碼中的問題,或者指出我正確解決這個問題,我將不勝感激。謝謝!公共類實現runnable將無法啓動Android
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_directory);
final ListView fileDirectory=(ListView) findViewById(R.id.DirectoryView);
final ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1, SocketConnection.remoteList);
fileDirectory.setAdapter(adapter);
fileDirectory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(FileDirectory.this, "The click works", Toast.LENGTH_SHORT).show();
FileDirectory.listItem=i;
new Connect2().execute("");
adapter.notifyDataSetChanged();
}
});
}
public class Connect2 extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params){
runConnectCode();
return null;
}
}
public void runConnectCode(){
String itemTemp=SocketConnection.remoteList.toArray()[FileDirectory.listItem].toString();
SocketConnection.PW.write(itemTemp);
//SocketConnection.DOS.flush();
SocketConnection.currentF.concat("\\"+itemTemp);
SocketConnection.PW.write(SocketConnection.currentF);
//SocketConnection.DOS.flush();
try{
Object object=SocketConnection.OIS.readObject();
SocketConnection.remoteList=(ArrayList<String>) object;
} catch(IOException e){
System.out.println(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}//run()
'System.out'沒有按」不一定要去任何地方。使用'System.err'或者更好的'android.util.Log'' – laalto