我有类扩展线程它获取数据库中的行数。 on oncreate方法我启动extends类并使用该方法名称setupListView()之后使用线程类的输出,但问题是我发现该方法在线程类之前运行。我不知道如何
线程类
public class array1 extends Thread{
String page="";
public void run(){
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http://192.168.43.16/marche/arraysize.php");
HttpResponse response = client.execute(request);
in = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
page = sb.toString();
String s11[]= page.split(System.getProperty("line.separator"));
s2=Integer.parseInt(s11[0]);
System.out.println(s2);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
的OnCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array1 www=new array1();
www.start();
Sendfeed1 pro1=new Sendfeed1();
pro1.start();
Sendfeed pro=new Sendfeed();
pro.start();
mSmoothInterpolator = new AccelerateDecelerateInterpolator();
mHeaderHeight = getResources().getDimensionPixelSize(R.dimen.header_height);
mMinHeaderTranslation = -mHeaderHeight + getActionBarHeight();
mListView = (ListView) findViewById(R.id.listview);
mHeader = findViewById(R.id.header);
mHeaderPicture = (KenBurnsView) findViewById(R.id.header_picture);
mHeaderPicture.setResourceIds(R.drawable.picture0, R.drawable.picture1);
mHeaderLogo = (ImageView) findViewById(R.id.header_logo);
mActionBarTitleColor = getResources().getColor(R.color.actionbar_title_color);
mSpannableString = new SpannableString(getString(R.string.noboringactionbar_title));
mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(mActionBarTitleColor);
setupActionBar();
setupListView();
}
答案 0 :(得分:1)
当你说www.start()时,你遇到的问题是你有2个线程吗?创建一个新线程(Array1)的UI线程;并不意味着您的UI线程将等待它,直到结束UI线程继续执行。现在两者都在CPU上运行“并行”。因此,您必须通知UI线程后台任务已完成。一旦run()方法完成,只需通知它。如何操作可以使用LocalBroadcastManager将结果传递到UI线程结果,或者如果要从Array1线程修改UI,则使用Handler。
一个建议还可以看一下AsyncTask类对你有所帮助。
我希望这有帮助。