无法在runonuithread中将值设置为textview

时间:2016-07-05 05:08:29

标签: java android multithreading textview runnable

public class PerformanceDashboard extends MotherActivity {

String dashboardData;
int SELECTED_PAGE, SEARCH_TYPE, TRAY_TYPE;
List<String[]> cachedCounterUpdates = new ArrayList<String[]>();
List<DasDetails> docList = new ArrayList<DasDetails>();
ListView listViewDashboard;
DataAdapter dataAdap = new DataAdapter();
TextView noOfItems, userCount, totalLoginTime;
int itemsTotal = 0, userTotal = 0, totalTime = 0;
String KEYWORD = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (App.isTestVersion) {
        Log.e("actName", "StoreOut");
    }

    if (bgVariableIsNull()) {
        this.finish();
        return;
    }

    setContentView(R.layout.dashboard);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setProgressBarIndeterminateVisibility(false);
    lytBlocker = (LinearLayout) findViewById(R.id.lyt_blocker);
    listViewDashboard = (ListView) findViewById(R.id.dashboard_listview);
    noOfItems = ((TextView) findViewById(R.id.noOfItems));
    userCount = ((TextView) findViewById(R.id.userCount));
    totalLoginTime = ((TextView) findViewById(R.id.totalLoginTime));

    new DataLoader().start();
    listViewDashboard.setAdapter(dataAdap);

    System.out.println("PerformanceDashboard. onCreate processOutData() -- item total " + itemsTotal); //0 i am not getting that adapter value i.e. 6 
    System.out.println("PerformanceDashboard. onCreate processOutData() -- user total " + userTotal); //0 i am not getting that adapter value i.e. 4
    System.out.println("PerformanceDashboard. onCreate processOutData() -- total total " + totalTime); //0 i am not getting that adapter value i.e. 310

}

private class DataAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return docList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        LayoutInflater li = getLayoutInflater();
        if (convertView == null)
            convertView = li.inflate(R.layout.dashboard_item, null);

        final DasDetails item = docList.get(position);

        ((TextView) convertView.findViewById(R.id.cMode))
                .setText(item.cMode);
        ((TextView) convertView.findViewById(R.id.noOfItems))
                .setText(item.totPickItemCount);
        ((TextView) convertView.findViewById(R.id.userCount))
                .setText(item.userCount);
        ((TextView) convertView.findViewById(R.id.totalLoginTime))
                .setText(item.totLoginTime);

        TextView textView = ((TextView) convertView
                .findViewById(R.id.avgSpeed));
        Double s = Double.parseDouble(item.avgPickingSpeed);
        textView.setText(String.format("%.2f", s));
        if (position == 0 || position == 2 || position == 4) {
            convertView.setBackgroundColor(getResources().getColor(
                    R.color.hot_pink));
        } else if (position == 1 || position == 3 || position == 5) {
            convertView.setBackgroundColor(getResources().getColor(
                    R.color.lightblue));
        }
        return convertView;
    }
}

class ErrorItem {

    String cMode, dDate, userCount, totLoginTime, totPickItemCount,
            avgPickingSpeed;

    public ErrorItem(HashMap<String, String> row) {
        cMode = row.get(XT.MODE);
        dDate = row.get(XT.DATE);
        userCount = row.get(XT.USER_COUNT);
        totLoginTime = row.get(XT.TOT_LOGIN_TIME);
        totPickItemCount = row.get(XT.TOT_PICK_ITEM_COUNT);
        avgPickingSpeed = row.get(XT.AVG_PICKING_SPEED);

    }

}

private class DataLoader extends Thread {

    @Override
    public void run() {
        super.run();

        System.out.println("DataLoader dashboard");

        List<NameValuePair> param = new ArrayList<NameValuePair>();

        param.add(new BasicNameValuePair(C.PRM_IDX, C.GET_SUMMARY));
        param.add(new BasicNameValuePair(C.PRM_HDR_DATA, "2016-07-04")); // yyyy-mm-dd

        toggleProgressNoUINoBlock(true);
        final String result = callService(C.WS_ST_PERFORMANCE_DASHBOARD,
                param);

        if (!App.validateXmlResult(actContext, null, result, true))
            return;

        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                Runnable r = new Runnable() {

                    @Override
                    public void run() {
                        dataAdap.notifyDataSetChanged();
                        toggleProgressNoUINoBlock(false);
                    }
                };

                dashboardData = result;
                processOutData(r);

            }

        });
    }
}

private String callService(String serviceName, List<NameValuePair> params) {
    String result = ws.callService(serviceName, params);
    return result;
}

private void processOutData(final Runnable rAfterProcessing) {

    if (dashboardData == null || dashboardData.length() == 0)
        return;

    new Thread() {
        @Override
        public void run() {
            super.run();

            final List<HashMap<String, String>> dataList = XMLfunctions
                    .getDataList(dashboardData, new String[] { XT.MODE,
                            XT.DATE, XT.USER_COUNT, XT.TOT_LOGIN_TIME,
                            XT.TOT_PICK_ITEM_COUNT, XT.AVG_PICKING_SPEED });

            final List<DasDetails> tempList = new ArrayList<DasDetails>();

            for (int i = 0; i < dataList.size(); i++) {

                int pos = docExists(tempList, dataList.get(i).get(XT.MODE));
                if (pos == -1) { 
                    if (SEARCH_TYPE == 0
                            || KEYWORD.equals("")
                            || (SEARCH_TYPE == 1 && dataList.get(i)
                                    .get(XT.CUST_NAME).contains(KEYWORD))
                            || (SEARCH_TYPE == 2 && dataList.get(i)
                                    .get(XT.DOC_NO).contains(KEYWORD))) {
                        DasDetails doc = new DasDetails(dataList.get(i));

                        int cachePos = getPosInCachedCounterUpdates(doc.cMode);
                        if (cachePos != -1) {
                            if (cachedCounterUpdates.get(cachePos)[1]
                                    .equals(doc.dDate))
                                cachedCounterUpdates.remove(cachePos);
                            else
                                doc.dDate = cachedCounterUpdates
                                        .get(cachePos)[1];
                        }

                        tempList.add(doc);
                        pos = tempList.size() - 1;
                    }

                }

                if (pos == -1)
                    continue; 
            }

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    docList = tempList;
                    rAfterProcessing.run();
                    logit("processOutData", "Processing OVER");
                }
            });
            for (int i = 0; i < docList.size(); i++) {
                itemsTotal = itemsTotal+ Integer.parseInt(docList.get(i).totPickItemCount);

                userTotal = userTotal + Integer.parseInt(docList.get(i).userCount);

                totalTime = totalTime + Integer.parseInt(docList.get(i).totLoginTime);

            }
            System.out.println("PerformanceDashboard.processOutData() -- fINAL item TOTAL " + itemsTotal);  // 6 i have data here but i need this data in my oncreate but not getting why?????
            System.out.println("PerformanceDashboard.processOutData() -- userTotal TOTAL "  + userTotal);   //4
            System.out.println("PerformanceDashboard.processOutData() -- totalTime  TOTAL " + totalTime);   //310
             noOfItems.setText(itemsTotal);   // crashing with null pointer exception
            // userCount.setText(userTotal);
            // totalLoginTime.setText(totalTime);
        };
    }.start();

}

private class DasDetails {

    public String cMode, dDate, userCount, totLoginTime, totPickItemCount,
            avgPickingSpeed;

    public DasDetails(HashMap<String, String> data) {
        cMode = data.get(XT.MODE);
        dDate = data.get(XT.DATE);
        userCount = data.get(XT.USER_COUNT);
        totLoginTime = data.get(XT.TOT_LOGIN_TIME);
        totPickItemCount = data.get(XT.TOT_PICK_ITEM_COUNT);
        avgPickingSpeed = data.get(XT.AVG_PICKING_SPEED);

    }
}

public Integer docExists(List<DasDetails> list, String docNo) {

    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).cMode.equals(docNo))
            return i;
    }
    return -1;
}

private int getPosInCachedCounterUpdates(String docNo) {
    for (int i = 0; i < cachedCounterUpdates.size(); i++) {
        if (cachedCounterUpdates.get(i)[0].equals(docNo))
            return i;
    }
    return -1;
}

}

以上代码请仔细阅读,如果需要澄清,请告诉我。我无法设置&#34; itemsTotal&#34;值为&#34; noOfIttems&#34; TextView中。我添加了评论。请帮我解决这个问题。 提前谢谢。

1 个答案:

答案 0 :(得分:3)

请检查你的noOfItems textView的id。 TextView为空。