java - 无法在 runonuithread 中为 textview 设置值

标签 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;
}

}

这是上面的代码,请仔细阅读它,如果需要任何说明,请告诉我。我无法将“itemsTotal”值设置为“noOfItems” TextView 。我已经添加了评论。请帮助我解决这个问题。 提前致谢。

最佳答案

请检查您的 noOfItems textView 的 ID。 TextView 为空。

关于java - 无法在 runonuithread 中为 textview 设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38195538/

相关文章:

android - 获取 ConstraintLayout 中元素之间的所有空闲空间

Android strace(或任何 linux 二进制文件)执行

c++ - 为什么在Release模式下的while循环中我无法逃脱?

c# - 使用 Invoke 方法从外部线程关闭窗体

java - 我如何在 asynctask 中创建 arrayadapter。执行后

java - 如何获取从不在类路径中的 jar 加载的任何类作为流?

java - SWT 中的字形指标?

java - 任何可以调用Java代码的 Node 模块

java - 向左/向右滚动到新页面

java - 是否可以挂起子线程然后回调父线程,然后再次恢复子线程