如何在FOR循环中从后台线程创建Intent

时间:2014-04-18 10:38:16

标签: android android-intent background-thread

for(i=0;i<arrayLength;i++) {
                    final int j=i;
                    LinearLayout childLinearLayout = new LinearLayout(getApplicationContext());
                    childLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
                    childLinearLayout.setPadding(0, 5, 0, 5);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT, 
                            LayoutParams.WRAP_CONTENT                               
                    );
                    params.setMargins(0, 0, 0, 10);
                    childLinearLayout.setLayoutParams(params);
                    childLinearLayout.setBackgroundResource(R.color.mainColor);
                    childLinearLayout.setBaselineAligned(false);
                    childLinearLayout.setWeightSum(100);
                    parentLayout.addView(childLinearLayout);

                    TextView companyName= new TextView(getApplicationContext());
                    LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(
                            0,LayoutParams.MATCH_PARENT, 50);
                    companyName.setLayoutParams(param1);
                    companyName.setTextSize(20);
                    companyName.setPadding(5, 5, 0, 0);
                    companyName.setTextColor(getResources().getColor(R.color.whiteColor));
                    companyName.setText(quotesJsonArray.getJSONObject(i).getString("company"));
                    childLinearLayout.addView(companyName);

                    TextView companyFare= new TextView(getApplicationContext());
                    LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
                            0,LayoutParams.MATCH_PARENT, 20);
                    companyFare.setLayoutParams(param2);
                    companyFare.setPadding(0, 5, 0, 0);
                    companyFare.setTextColor(getResources().getColor(R.color.whiteColor));
                    companyFare.setTextSize(20);
                    companyFare.setText(quotesJsonArray.getJSONObject(i).getString("company_fare_amount"));
                    childLinearLayout.addView(companyFare);

                    Button acceptBtn = new Button(getApplicationContext());
                    acceptBtn.setLayoutParams(new LayoutParams(0, 50));
                    LinearLayout.LayoutParams param3 = new LinearLayout.LayoutParams(
                            0,LayoutParams.MATCH_PARENT, 30);
                    acceptBtn.setLayoutParams(param3);
                    acceptBtn.setPadding(0, 0, 0, 5);
                    acceptBtn.setId(Integer.parseInt(quotesJsonArray.getJSONObject(i).getString("company_request_id")));
                    acceptBtn.setBackgroundResource(R.drawable.a15);
                    childLinearLayout.addView(acceptBtn);

                    acceptBtn.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {

                            try {
                                Toast.makeText(Quote.this, String.valueOf(Integer.parseInt(quotesJsonArray.getJSONObject(j).getString("company_request_id"))), Toast.LENGTH_LONG).show();
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                            Intent intent = new Intent(Quote.this,ConfirmFare.class);
                            try {
                                intent.putExtra("company_request_id", quotesJsonArray.getJSONObject(j).getString("company_request_id"));
                            } catch (NumberFormatException e1) {
                                e1.printStackTrace();
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }
                            startActivity(intent);
                            finish();

                        }
                      });
                }

我是Android开发的新手。这是我的代码,我无法转移到另一个活动。我正在for循环中的后台线程函数中创建Intent对象。如何从这个区域调用Intent。这个for循环在onPostExecute()函数中。从这种背景类方法向UI添加视图是否正确。

Logcat文件: -

    04-18 18:17:15.427: V/Provider/Settings(15346): invalidate [system]: current 251 != cached 0
04-18 18:17:15.438: V/Provider/Settings(15346): from db cache, name = sound_effects_enabled , value = 0
04-18 18:17:15.448: D/skia(15346): Flag is not 10
04-18 18:17:15.626: V/InputMethodManager(15346): onWindowFocus: null softInputMode=288 first=true flags=#1810500
04-18 18:17:15.627: V/InputMethodManager(15346): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{41c29030 V.E..... R.....ID 0,0-1080,1920} ic=null tba=android.view.inputmethod.EditorInfo@41c2c1a0 controlFlags=#104
04-18 18:17:15.629: V/InputMethodManager(15346): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@41c15d38 com.android.inputmethod.latin/.LatinIME #2179}
04-18 18:17:15.689: D/OpenGLRenderer(15346): Flushing caches (mode 0)
04-18 18:17:16.063: D/OpenGLRenderer(15346): Flushing caches (mode 0)
04-18 18:17:16.138: D/dalvikvm(15346): threadid=15: interp stack at 0x5e3fc000
04-18 18:17:16.139: D/jdwp(15346): sendRequest : Len=0x1D
04-18 18:17:16.141: D/jdwp(15346): processIncoming
04-18 18:17:16.141: D/jdwp(15346): handlePacket : cmd=0x9, cmdSet=0x9, len=0x13, id=0x59E, flags=0x0, dataLen=0x8
04-18 18:17:16.143: D/jdwp(15346): processIncoming
04-18 18:17:16.143: D/jdwp(15346): handlePacket : cmd=0x5, cmdSet=0xB, len=0x13, id=0x59F, flags=0x0, dataLen=0x8
04-18 18:17:16.144: D/jdwp(15346): processIncoming
04-18 18:17:16.144: D/jdwp(15346): handlePacket : cmd=0x1, cmdSet=0x9, len=0x13, id=0x5A0, flags=0x0, dataLen=0x8
04-18 18:17:16.145: D/jdwp(15346): processIncoming
04-18 18:17:16.145: D/jdwp(15346): handlePacket : cmd=0x2, cmdSet=0x9, len=0x1B, id=0x5A1, flags=0x0, dataLen=0x10
04-18 18:17:16.147: D/jdwp(15346): processIncoming
04-18 18:17:16.148: D/jdwp(15346): handlePacket : cmd=0x4, cmdSet=0xB, len=0x13, id=0x5A2, flags=0x0, dataLen=0x8
04-18 18:17:16.148: D/jdwp(15346):     --> RUNNING, Not SUSPENDED
04-18 18:17:16.149: D/jdwp(15346): processIncoming
04-18 18:17:16.149: D/jdwp(15346): handlePacket : cmd=0x4, cmdSet=0xB, len=0x13, id=0x5A3, flags=0x0, dataLen=0x8
04-18 18:17:16.149: D/jdwp(15346):     --> RUNNING, Not SUSPENDED

0 个答案:

没有答案