线程开始执行很晚我不知道为什么

时间:2015-08-12 17:34:25

标签: android

我有两个数组,我想设置动态长度。每个长度来自数据库中的行数。

我有一个本地类,它扩展了名为array1的Thread,它获取了数据库中的行数,并创建了一个名为b的数组,其长度是类array1的输出和我从数组Cat_Name获取数据并将其设置在数组b上。但是当我将b传递给显示数据的main1类时,它会指出array1值为空。

   public class Main extends Activity {

private static final String TAG = "NoBoringActionBarActivity";
private int mActionBarTitleColor;
private int mActionBarHeight;
private int mHeaderHeight;
private int mMinHeaderTranslation;
private ListView mListView;
public KenBurnsView mHeaderPicture;
private ImageView mHeaderLogo;

private View mHeader;
private View mPlaceHolderView;
private AccelerateDecelerateInterpolator mSmoothInterpolator;

private RectF mRect1 = new RectF();
private RectF mRect2 = new RectF();

private AlphaForegroundColorSpan mAlphaForegroundColorSpan;
private SpannableString mSpannableString;

private TypedValue mTypedValue = new TypedValue();


String [] s1=new String [1];
String [] logo =new String [8];//={R.drawable.accessories,R.drawable.accessories,R.drawable.accessories,R.drawable.accessories,R.drawable.accessories,R.drawable.accessories,R.drawable.accessories,R.drawable.accessories};// {"http://192.168.43.16/marche/accessories.png","http://192.168.43.16/marche/accessories.png","http://192.168.43.16/marche/accessories.png","http://192.168.43.16/marche/accessories.png","http://192.168.43.16/marche/accessories.png","http://192.168.43.16/marche/accessories.png","http://192.168.43.16/marche/accessories.png","http://192.168.43.16/marche/accessories.png"};
String [] Cat_Name=new String [8];//={"accessories","clothes","glasses","Laptob","mobiles","perfum","toys","watches"};
private static final int MENU_VIEW1 = Menu.FIRST;
private static final int MENU_UPLOAD = Menu.FIRST +1;
@Override
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();


  //     ImageButton im = new ImageButton(context);
}




public class Sendfeed 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/category_display.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 [] s= page.split(System.getProperty("line.separator"));
         for(int i=0;i<s.length;i++){
             Cat_Name[i]=s[i];
         }

        } catch (Exception e) {
        e.printStackTrace();
        } finally {
        if (in != null) {
        try {
        in.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }

    }

    }



}



public class Sendfeed1 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/categoryIcon_display.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 [] s= page.split(System.getProperty("line.separator"));
         for(int i=0;i<s.length;i++){
             logo[i]=s[i];
 //                  System.out.println(logo[i]);
         }

        } catch (Exception e) {
        e.printStackTrace();
        } finally {
        if (in != null) {
        try {
        in.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }

    }

    }



}





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();

         s1= page.split(System.getProperty("line.separator"));
         System.out.println(s1.length);
         System.out.println(s1[0]);
         System.out.println(Integer.parseInt(s1[0]));
        } catch (Exception e) {
        e.printStackTrace();
        } finally {
        if (in != null) {
        try {
        in.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }

    }

    }



}






@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
      //do your action here.
        Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();

        break;

    }

    return true;
}
private void setupListView() {
    ArrayList<String> FAKES = new ArrayList<String>();
    getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
    mListView.addHeaderView(mPlaceHolderView);
    int iii=Integer.parseInt(s1[0]);        
    String  [] b = Arrays.copyOf(Cat_Name, iii);
    System.out.println(iii+"hhhhhh");
    Main1 adapter = new Main1(Main.this,  b, logo);
    mListView.setAdapter(adapter);

    //     mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

           // Toast.makeText(Main.this, "You Clicked at " +Cat_Name[ position-1], Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Main.this,Shops.class);
            intent.putExtra("the shop category", Cat_Name[position-1]);
              startActivity(intent);


        }
    });

    mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            int scrollY = getScrollY();
            //sticky actionbar
            mHeader.setTranslationY(Math.max(-scrollY, mMinHeaderTranslation));
            //header_logo --> actionbar icon
            float ratio = clamp(mHeader.getTranslationY() / mMinHeaderTranslation, 0.0f, 1.0f);
            interpolate(mHeaderLogo, getActionBarIconView(), mSmoothInterpolator.getInterpolation(ratio));
            //actionbar title alpha
            //getActionBarTitleView().setAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));
            //---------------------------------
            //better way thanks to @cyrilmottier
            setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));
        }
    });
}

private void setTitleAlpha(float alpha) {
    mAlphaForegroundColorSpan.setAlpha(alpha);
    mSpannableString.setSpan(mAlphaForegroundColorSpan, 0, mSpannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    getActionBar().setTitle(mSpannableString);
}

public static float clamp(float value, float min, float max) {
    return Math.max(min,Math.min(value, max));
}

private void interpolate(View view1, View view2, float interpolation) {
    getOnScreenRect(mRect1, view1);
    getOnScreenRect(mRect2, view2);

    float scaleX = 1.0F + interpolation * (mRect2.width() / mRect1.width() - 1.0F);
    float scaleY = 1.0F + interpolation * (mRect2.height() / mRect1.height() - 1.0F);
    float translationX = 0.5F * (interpolation * (mRect2.left + mRect2.right - mRect1.left - mRect1.right));
    float translationY = 0.5F * (interpolation * (mRect2.top + mRect2.bottom - mRect1.top - mRect1.bottom));

    view1.setTranslationX(translationX);
    view1.setTranslationY(translationY - mHeader.getTranslationY());
    view1.setScaleX(scaleX);
    view1.setScaleY(scaleY);
}

private RectF getOnScreenRect(RectF rect, View view) {
    rect.set(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
    return rect;
}

public int getScrollY() {
    View c = mListView.getChildAt(0);
    if (c == null) {
        return 0;
    }

    int firstVisiblePosition = mListView.getFirstVisiblePosition();
    int top = c.getTop();

    int headerHeight = 0;
    if (firstVisiblePosition >= 1) {
        headerHeight = mPlaceHolderView.getHeight();
    }

    return -top + firstVisiblePosition * c.getHeight() + headerHeight;
}

private void setupActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setIcon(R.drawable.ic_transparent);
    actionBar.setDisplayHomeAsUpEnabled(true);


    //getActionBarTitleView().setAlpha(0f);
}

private ImageView getActionBarIconView() {
    return (ImageView) findViewById(android.R.id.home);
}

/*private TextView getActionBarTitleView() {
    int id = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    return (TextView) findViewById(id);
}*/

public int getActionBarHeight() {
    if (mActionBarHeight != 0) {
        return mActionBarHeight;
    }
    getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true);
    mActionBarHeight = TypedValue.complexToDimensionPixelSize(mTypedValue.data, getResources().getDisplayMetrics());
    return mActionBarHeight;
}


public boolean onCreateOptionsMenu1(Menu menu) {
    menu.add(0, MENU_VIEW1, 0, "Logout");
    menu.add(0, MENU_UPLOAD, 0, "Help");
    return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
    case MENU_VIEW1:
        // Code for menu option View
        Intent intent = new Intent(Main.this,Main.class);
      startActivity(intent);

        return true;
    case MENU_UPLOAD:
        // Code for menu option Upload
        return true;

    default:
        return false;
    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

我的问题是,课程array1的执行迟到了,我不知道为什么。

1 个答案:

答案 0 :(得分:0)

强烈建议不要在Android中处理Java线程,为什么不使用AsyncTask?为什么不尝试改造http://square.github.io/retrofit/来调用您的Web服务?这只是一个建议