0
ublic class SectionsList extends LinearLayout implements OnOffChangeListener,OnClickListener,MessageListener{

private Context mContext;
private Dialog dialog;
private List<TextView> tList = new ArrayList<TextView>();
private List<EditValueButton> editList = new ArrayList<EditValueButton>();
private List<OnOffButton> onOffList = new ArrayList<OnOffButton>();
private List<String> sectionNames = NewsReader.getInstance().getSectionNamesListWithFunStories();
private final int ChangeBannerDeliveryBetweenTimeDialog_ID = 1;
private final int ChangeBannerDeliveryBetweenDateTimeDialog_ID = 2;
private Handler mHandler;
private EditValueButton editButton;
private long startTime;
private View setView;
private OnOffButton Id;
private Boolean sectionOn = false;
private Boolean setOff;
private Boolean checkSection = false;
private String activeSection;
private OnOffButton onOff;
private boolean cancel;
private TextView sectionName;
private TableLayout table;
private Resources resources;
private int textNormalColor;
private HashMap<String, Time> getSectiontime;
private HashMap<String, Time> hm = new HashMap<String, Time>();

@Override
public boolean handleMessage(Message msg) {
    boolean ret;
    Bundle  bundle = msg.getData();
    switch ( msg.what)
    {
    case Constants.message_SetBannerTime:
        startTime = bundle.getLong(Constants.KEY_ARG_1);
        setDisplayDuringLabel(startTime, setView);
        ret = true;
        break;
    case Constants.message_setOff:
        cancel = bundle.getBoolean(Constants.KEY_ARG_1);
        if(cancel){
        setOff();
        ret = true;
        break;
    }
    default:
    {
        ret = false;
        break;
    }
}
    return ret;
}

public SectionsList(Context context) {
    super(context);
    mContext = context;
    init(context, null);
}

public SectionsList(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    init(context, attrs);
}


private void init(Context context,  AttributeSet attrs) {

    //new CustomTask().execute();

    Log.d("secnames", "section names" + sectionNames);
    getSectiontime = PersistedScheduledBannerStore.getInstance().getScheduledBanners();
    resources = context.getResources();
    textNormalColor = resources.getColor(R.color.app_button_normal_text_color);
    if(getSectiontime != null){hm = getSectiontime;}
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.sectiontable, this, true);
    table = (TableLayout) findViewById(R.id.mainTable);
    for(int i =0; i< sectionNames.size();i++){
                TableRow tr = new TableRow(context);
                View line = new View(context);
                line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
                line.setBackgroundColor(textNormalColor);
                line.setPadding(0, 10, 0, 10);

                layoutInflater.inflate(R.layout.sectiontablerow, tr);
                table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
                table.addView(line);

                setIds(tr);
                setTagstoViews(i);
                addViewstoList();
                setListeners();
                checkIfSectionIsOn(i);
                tList.get(i).setText("sai ram");
            }

}



private void loopThroughViews(Context context, LayoutInflater layoutInflater) {

}

private void checkIfSectionIsOn(int i) {
    if(getSectiontime.containsKey(sectionNames.get(i))){
        String key = sectionNames.get(i);
        Time value = getSectiontime.get(key);
        editList.get(i).setText(DateFormat.format("h:mm a", value).toString());
        onOff.setOn(true);  
    }else{
        editList.get(i).setText("-:-");
    }
}


public void onOffChanged(OnOffButton button, boolean isOn) {
    setView = button;

    if(isOn){
        sectionOn = true;
    }
    else if (!isOn){
        sectionOn = false;
        makeSectionActive(button);
        hm.remove(activeSection);
        getSectiontime.remove(activeSection);
    }
}


 protected Dialog onCreateDialog(int id) {
        switch (id) {
        case ChangeBannerDeliveryBetweenTimeDialog_ID:
        {
            long            curStartTime = 0;
            dialog = new ChangeBannerTimeDialog(getContext(), curStartTime, mHandler,0);                
            break;
        }
        case ChangeBannerDeliveryBetweenDateTimeDialog_ID:
        {
            long            curStartTime = 0;
            dialog = new ChangeBannerdateTimeDialog(getContext(), curStartTime, mHandler, 0);           
            break;
        }
        default:
            dialog = null;
    }
    return dialog;
}

private void setDisplayDuringLabel(long startTime, View v){
    changeTimeText(v, DateFormat.format("h:mm a", startTime).toString());
    Time time = new Time(startTime);
    if(sectionOn){
        hm.put(activeSection, time);
    }

}

@Override
public void onClick(View v) {
    setView = v;
    if(v.getId() == R.id.sectionOnOff){
        if(sectionOn){
            TextView tt = tList.get(Integer.parseInt(v.getTag().toString()));
            String secName = tt.getText().toString();
            showDateTimeDialogIfHoroscopes(secName);
            setOff = true;
            Id = onOffList.get(Integer.parseInt(v.getTag().toString()));
            Id.setOn(true);
            makeSectionActive(v);
        }
    }
    else if(v.getId() == R.id.editTime){
        setView = v;
        Id = onOffList.get(Integer.parseInt(v.getTag().toString()));
        if(Id.isOn()){
        TextView tt = tList.get(Integer.parseInt(v.getTag().toString()));
        String secName = tt.getText().toString();
        showDateTimeDialogIfHoroscopes(secName);
        setOff = false;
        Id.setOn(true);
        makeSectionActive(v);
        }
    }
}

private void showDateTimeDialogIfHoroscopes(String secName) {
    if(secName.equals("Horoscopes")){
        onCreateDialog(ChangeBannerDeliveryBetweenDateTimeDialog_ID).show();
    }else{
        onCreateDialog(ChangeBannerDeliveryBetweenTimeDialog_ID).show();
    }
}

private void setOff() {
    if(setOff){
        Id.setOn(false);
    }
    else if(!setOff){
        Id.setOn(true);
    }
}

private void setIds(TableRow tr){
    onOff = (OnOffButton) tr.findViewById(R.id.sectionOnOff);
    editButton = (EditValueButton) tr.findViewById(R.id.editTime);
    sectionName = (TextView) tr.findViewById(R.id.sectionName);
}

private void setTagstoViews(int i){
    onOff.setTag(""+i);
    editButton.setTag(""+i);
}
private void setListeners(){
    editButton.setOnClickListener(this);
    onOff.setOnOffChangeListener(this);
    onOff.setOnClickListener(this);
}


public HashMap<String, Time> getBannerSectionwithTime(){
    return hm;
}
private void addViewstoList(){
    onOffList.add(onOff);
    editList.add(editButton);
    tList.add(sectionName);
}

private void makeSectionActive(View v){
    TextView tt = tList.get(Integer.parseInt(v.getTag().toString()));
    activeSection = tt.getText().toString();
}

private void changeTimeText(View v, String text){
    EditValueButton tt = editList.get(Integer.parseInt(v.getTag().toString()));
    tt.setText(text);
}

public  boolean checkSectionOn(){
    return checkSection;
}   

private class CustomTask extends AsyncTask<String, Void, Boolean>{
    private ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        dialog=ProgressDialog.show(getContext(), "", "Loading...");
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        dialog.dismiss();
    }

    @Override
    protected Boolean doInBackground(String... params) {
        return null;
    }

}

}

I want to use Async task to put all Ui in postexecute and loops in doinbackground? In loop i have my table row which consumens 5secs to 8sec to load so can we somehow add the loop in do in background? Any help would be appreciated. Please provide code sample for better understanding

4

1 回答 1

0

首先定义什么是你的耗时任务。如果你想在 asyncTask 中创建你的表,这里有一个提示如何去做。首先,您需要在屏幕外创建整个表格,然后将其添加到您的布局中

private TableLayout table;

private void init(){
    new CustomTask().execute();
}

private class CustomTask extends AsyncTask<String, Void, Boolean>{
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog=ProgressDialog.show(getContext(), "", "Loading...");
}


@Override
protected Boolean doInBackground(String... params) {
    // Here is the part where you create your table and do extensive work
    // For example
    for(int i =0; i< sectionNames.size();i++){
            TableRow tr = new TableRow(context);
            View line = new View(context);
            line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
            line.setBackgroundColor(textNormalColor);
            line.setPadding(0, 10, 0, 10);

            layoutInflater.inflate(R.layout.sectiontablerow, tr);
            table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
            table.addView(line);

            setIds(tr);
            setTagstoViews(i);
            addViewstoList();
            setListeners();
            checkIfSectionIsOn(i);
            tList.get(i).setText("sai ram");
        }
    return null;
}

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);
    //And now add your tableLayout in your Layout
    layout.addView(tableLayout);
    // Adjust layout params per need 
    dialog.dismiss();
}

}

根据您的需要调整所有这些。

希望这对您有所帮助并享受您的工作。

于 2013-05-20T23:09:54.737 回答