runOnUiThread后无法加载expandable listview

时间:2017-10-30 20:54:54

标签: android multithreading expandablelistview expandablelistadapter android-runonuithread

我正在尝试在可扩展的列表视图上绑定数据。由于在从数据库填充映射期间主线程上的负载过多,因此调用了新线程。

  

我的问题是,在新线程之后没有显示可扩展列表视图   执行但如果我在适配器初始化上设置了一个断点,   它工作得很好..我试过notifyDataSetChanged()但没有运气

enter image description here

活动

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            return;
        }
         selectedvalue = extras.getString("selected_data");
        if (selectedvalue != null) {
            TextView textView=(TextView)findViewById(R.id.textView2SelectedData);
            textView.setText(selectedvalue);
            textView.setBackgroundResource(R.color.colorAccent);
        }
        fillData();
        ExpandableListView expandableListview =(ExpandableListView)findViewById(R.id.ExpandablelistSelectedData);
        ExpandableListAdapter expandableListAdapter=new CustomAdapterExpandableList(this,manufacturerUnique,relatedMedicine);
        expandableListview.setAdapter(expandableListAdapter);
    }
    public void fillData(){
        manufacturerUnique=new ArrayList<>();
        relatedMedList=new ArrayList<>();
        relatedMedicine=new HashMap<>();
        dbHelper=new DBHelper(this);
        mProgressDialog = ProgressDialog.show(this, "Please wait","Loading...", false);
        new Thread() {
            @Override
            public void run() {
                try {
                    manufacturerUnique=dbHelper.getManfacturerNameListusingBrandOrGenericName(selectedvalue);
                    System.out.println("manufacturerUnique size"+manufacturerUnique.size());
                    for(int i=0;i<manufacturerUnique.size();i++){
                        relatedMedicine.put(manufacturerUnique.get(i),dbHelper.getBrandNameUsingSelectedItemAndmanfacturerName(selectedvalue,manufacturerUnique.get(i)));
                        System.out.println("manufacturerUnique added:"+manufacturerUnique.get(i));
                    }
                    System.out.println("related:"+relatedMedicine.size());                    
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mProgressDialog.dismiss();
                        }
                    });
                } catch (final Exception ex) {
                    Log.i("---","Exception in thread");
                }
            }
        }.start();
    }
}

我在这里做错了什么?我非常感谢任何反馈或指出类似这样的问题,谢谢。

修改

public class ResultActivity extends AppCompatActivity {
    private DBHelper dbHelper=null;

    ExpandableListView expandableListview;
    String selectedvalue="";

    List<String> manufacturerUnique;
    List<String>relatedMedList;
    Map<String,ArrayList<String>>relatedMedicine;
    //ExpandableListAdapter expandableListAdapter;
    BaseExpandableListAdapter expandableListAdapter;
    private ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            return;
        }
         selectedvalue = extras.getString("selected_data");
        if (selectedvalue != null) {
            TextView textView=(TextView)findViewById(R.id.textView2SelectedData);
            textView.setText(selectedvalue);
            textView.setBackgroundResource(R.color.colorAccent);
        }
        fillData();
        ExpandableListView expandableListview =(ExpandableListView)findViewById(R.id.ExpandablelistSelectedData);
         expandableListAdapter=new CustomAdapterExpandableList(this,manufacturerUnique,relatedMedicine);
       // expandableListAdapter.notifyDataSetChanged();
        expandableListview.setAdapter(expandableListAdapter);
    }
    public void fillData(){
        manufacturerUnique=new ArrayList<>();
        relatedMedList=new ArrayList<>();
        relatedMedicine=new HashMap<>();
        dbHelper=new DBHelper(this);
        mProgressDialog = ProgressDialog.show(this, "Please wait","Loading...", false);
        new Thread() {
            @Override
            public void run() {
                try {

                    manufacturerUnique=dbHelper.getManfacturerNameListusingBrandOrGenericName(selectedvalue);
                    System.out.println("manufacturerUnique size"+manufacturerUnique.size());
                    for(int i=0;i<manufacturerUnique.size();i++){
                        relatedMedicine.put(manufacturerUnique.get(i),dbHelper.getBrandNameUsingSelectedItemAndmanfacturerName(selectedvalue,manufacturerUnique.get(i)));
                        System.out.println("manufacturerUnique added:"+manufacturerUnique.get(i));
                    }
                    System.out.println("related:"+relatedMedicine.size());
                   // expandableListAdapter.notifyDataSetChanged();// exception


                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mProgressDialog.dismiss();
                            expandableListAdapter.notifyDataSetChanged();




                        }
                    });
                } catch (final Exception ex) {
                    Log.i("---",ex.toString());
                }
            }
        }.start();


    }


}

0 个答案:

没有答案