所以我有一個AsyncTask與RunOnUiThread交互更改UI元素並添加TableRows問題是在TableRows中的監聽器。只有一個有效,但其他則不行。這是從我的代碼聽衆不在OnUiThread工作
public class SearchAsync extends AsyncTask<String, String, String> {
private Context mContext;
private Activity mAct;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
public void mytask(Context context, Activity act) {
mContext = context;
mAct = act;
}
@Override
protected String doInBackground(final String... arg0) {
String sc2 = "Alpha";
mAct.runOnUiThread(new Runnable() {
@Override
public void run() {
GridLayout gey = (GridLayout) mAct.findViewById(R.id.greylayout);
TableLayout tbly = (TableLayout) mAct.findViewById(R.id.listviewtable);{
final TableRow tbro = new TableRow(mContext);
TextView tev = new TextView(mContext);
tbro.setBackground(mAct.getResources().getDrawable(R.drawable.mybgbg));
tev.setTextColor(Color.WHITE);
tev.setText(sc2);
tbro.addView(tev);
tbro.setOnClickListener(new View.OnClickListener() { // this listener doesn't work.
public void onClick(View v) {
System.out.println("yey! it works");
}
});
tbro.setOnLongClickListener(new View.OnLongClickListener(){ // this listener doesn't work too.
@Override
public boolean onLongClick(View v){
System.out.println("yey! it works too!");
return true;
}
});
tbro.setOnTouchListener(new View.OnTouchListener() { // this listener works perfectly.
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
tbro.setBackground(mAct.getResources().getDrawable(R.drawable.mybgi));
} else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
tbro.setBackground(mAct.getResources().getDrawable(R.drawable.mybgbg));
}
return true;
}
});
tbro.setId(R.id.layoutx1);
tbro.setLongClickable(true);
tbro.setClickable(true);
tbly.addView(tbro);
}
}
}
TableRow tete = (TableRow)mAct.findViewById(R.id.layoutx1);
});
}
catch(JSONException e){
Log.e(TAG, "Json parsing error: " + e.getMessage());
);
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(null);
final String rslt;
rslt = result;
mAct.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast toast = Toast.makeText(mContext, rslt, Toast.LENGTH_SHORT);
toast.show();
}
});
}
因此,只有OnTouchListener作品簡單,但別人不..我不知道爲什麼。 這只是runOnUiThread和runonmainthread中的一個問題。
我試過代碼onPreExecute()方法和onPostExecute。沒有用。
但在MainActivity它工作得很好。
感謝您閱讀我的問題。
tbro.setLongClickable(真); 和 tbro.setClickable(true); 之前聽衆..當我編輯代碼在這裏,我忘了他們 – Nasreddine