2012-10-15 125 views
0
public class circularlistparsing extends ActivityGroup { 

public int currentPage = 1; 
public ListView lisView1; 
static final String KEY_ITEM = "docdetails"; 
static final String KEY_ITEM2 = "info"; 
static final String KEY_NAME1 = ""; 
static final String KEY_NAME = "heading"; 
static final String KEY_DATE = "date"; 
public Button btnNext; 
public Button btnPre; 
public static String url = "http://dev.taxmann.com/TaxmannService/TaxmannService.asmx/GetCircularList"; 
TextView txtreord; 
TextView totalpage; 
TextView pagenumber; 
ProgressDialog dialog; 
TextView title; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    txtreord = (TextView) findViewById(R.id.recored); 
    totalpage = (TextView) findViewById(R.id.totalpage); 
    pagenumber = (TextView) findViewById(R.id.pagenumber); 
    title = (TextView) findViewById(R.id.title); 

    title.setText("Cirrcular"); 
    // listView1 
    lisView1 = (ListView) findViewById(R.id.listView1); 

    // Next 
    btnNext = (Button) findViewById(R.id.btnNext); 
    // Perform action on click 
    btnNext.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      currentPage = currentPage + 1; 

      // new YourTask().execute(); 
      ShowData(); 
      pagenumber.setText("Of" + currentPage + "]"); 
     } 
    }); 

    // Previous 
    btnPre = (Button) findViewById(R.id.btnPre); 
    // Perform action on click 
    btnPre.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      currentPage = currentPage - 1; 

      // new YourTask().execute(); 
      ShowData(); 
      pagenumber.setText("Of" + currentPage + "]"); 
     } 
    }); 


    ShowData(); 
} 

public void ShowData() { 
    XMLParser parser = new XMLParser(); 
    String xml = parser.getXmlFromUrl(url); // getting XML 

    Document doc = parser.getDomElement(xml); // getting DOM element 

    NodeList nl = doc.getElementsByTagName(KEY_ITEM); 

    NodeList n2 = doc.getElementsByTagName(KEY_ITEM2); 

    int displayPerPage = 10; // Per Page 
    int TotalRows = nl.getLength(); 

    txtreord.setText(TotalRows + "Records|"); // number of records 

    int indexRowStart = ((displayPerPage * currentPage) - displayPerPage); 
    int TotalPage = 0; 
    if (TotalRows <= displayPerPage) { 
     TotalPage = 1; 
    } else if ((TotalRows % displayPerPage) == 0) { 
     TotalPage = (TotalRows/displayPerPage); 
    } else { 
     TotalPage = (TotalRows/displayPerPage) + 1; // 7 
     TotalPage = (int) TotalPage; // 7 
    } 

    totalpage.setText("Page[" + TotalPage); 

    int indexRowEnd = displayPerPage * currentPage; // 5 
    if (indexRowEnd > TotalRows) { 
     indexRowEnd = TotalRows; 
    } 

    // Disabled Button Next 
    if (currentPage >= TotalPage) { 
     btnNext.setEnabled(false); 
    } else { 
     btnNext.setEnabled(true); 
    } 

    // Disabled Button Previos 
    if (currentPage <= 1) { 
     btnPre.setEnabled(false); 
    } else { 
     btnPre.setEnabled(true); 
    } 

    // Load Data from Index 
    int RowID = 1; 
    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); 
    HashMap<String, String> map; 

    // RowID 
    if (currentPage > 1) { 
     RowID = (displayPerPage * (currentPage - 1)) + 1; 
    } 

    for (int i = indexRowStart; i < indexRowEnd; i++) { 
     Element e = (Element) nl.item(i); 

     Element e2 = (Element) n2.item(i); 

     String date = e2.getAttribute(KEY_DATE); 
     // adding each child node to HashMap key => value 

     map = new HashMap<String, String>(); 
     map.put("RowID", String.valueOf(RowID)); 

     map.put(KEY_DATE, date); 



     String mytime = date; 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd"); 
     Date myDate = null; 
     try { 
      myDate = dateFormat.parse(mytime); 

     } catch (ParseException t) { 
      t.printStackTrace(); 
     } catch (java.text.ParseException t) { 
      // TODO Auto-generated catch block 
      t.printStackTrace(); 
     } 

     SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     String finalDate = timeFormat.format(myDate); 

     // System.out.println("rrrrrrrrrrrrr"+finalDate); 

     String Heading = parser.getValue(e, KEY_NAME); 

     int a = Heading.indexOf("|"); 
     String beforeSubString = Heading.substring(0, a); 
     String afterSubString = Heading.substring(a, Heading.length()) 
       .replace("|", "") + "[" + finalDate + "]"; 
     // String 
     // final1="<b>"+beforeSubString+"<b>"+"|"+afterSubString.replace("|", 
     // "|\n") 
     // .replace("|", ""); 
     // String k=Html.fromHtml(final1).toString(); 
     // 
     // Html.fromHtml(final1); 

     map.put(KEY_NAME, beforeSubString); 
     map.put(KEY_NAME1, afterSubString); 

     // adding HashList to ArrayList 
     menuItems.add(map); 

     RowID = RowID + 1; 

    } 

    SimpleAdapter sAdap; 
    sAdap = new SimpleAdapter(circularlistparsing.this, menuItems, 
      R.layout.list_item, 
      new String[] { "RowID", KEY_NAME1, KEY_NAME }, new int[] { 
        R.id.ColRowID, R.id.ColName, R.id.textView1 }); 
    lisView1.setAdapter(sAdap); 

    lisView1.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 

      Intent i = new Intent(circularlistparsing.this, detail.class); 
      // sending data to new activity 
      // i.putExtra("product", product); 
      startActivity(i); 

     } 
    }); 

} 
} 

這是我的源代碼,本次活動的公共類circularlistparsing擴展的ActivityGroup 這是我的活動這Actvity加載onButton另一個類我想設置進度條加載數據的點擊,請告訴我如何實現這一點,我做了很多,但無法顯示進度欄我有asynk任務,然後也無法顯示。如何設置進度條

+0

想要顯示「進度條」或「進度對話框」嗎?你可以使用'Async Task'發佈你的'YourTask()'代碼。 –

回答

0

試試這個代碼,

/** Called when the activity is first created. */ 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  

    RelativeLayout fv = new RelativeLayout(this); 
    ProgressBar pb = new ProgressBar(this); 
    fv.setGravity(Gravity.CENTER); 
    fv.addView(pb); 
    setContentView(fv); 
} 

這項工作細跟我..

0
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
txtreord = (TextView) findViewById(R.id.recored); 
totalpage = (TextView) findViewById(R.id.totalpage); 
pagenumber = (TextView) findViewById(R.id.pagenumber); 
title = (TextView) findViewById(R.id.title); 

title.setText("Cirrcular"); 
// listView1 
lisView1 = (ListView) findViewById(R.id.listView1); 

// Next 
btnNext = (Button) findViewById(R.id.btnNext); 
// Perform action on click 
btnNext.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     currentPage = currentPage + 1; 

     // new YourTask().execute(); 
     ShowData(); 
     pagenumber.setText("Of" + currentPage + "]"); 
    } 
}); 

// Previous 
btnPre = (Button) findViewById(R.id.btnPre); 
// Perform action on click 
btnPre.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     currentPage = currentPage - 1; 

     // new YourTask().execute(); 
     ShowData(); 
     pagenumber.setText("Of" + currentPage + "]"); 
     } 
    }); 

    new NflAsyncTask().execute(); 

    } 
}  
public class NflAsyncTask extends AsyncTask<Void, Void, Void> { 

private ProgressDialog progressDialog; 

protected void onPreExecute() { 
    progressDialog = ProgressDialog.show(Activityname.this, 
      "Please wait", "Loading Data ...", true); 
} 

@Override 
protected Void doInBackground(Void... params) { 
    XMLParser parser = new XMLParser(); 
     String xml = parser.getXmlFromUrl(url); // getting XML 

     Document doc = parser.getDomElement(xml); // getting DOM element 

     NodeList nl = doc.getElementsByTagName(KEY_ITEM); 

     NodeList n2 = doc.getElementsByTagName(KEY_ITEM2); 

     int displayPerPage = 10; // Per Page 
     int TotalRows = nl.getLength(); 

     txtreord.setText(TotalRows + "Records|"); // number of records 

     int indexRowStart = ((displayPerPage * currentPage) - displayPerPage); 
     int TotalPage = 0; 
     if (TotalRows <= displayPerPage) { 
      TotalPage = 1; 
     } else if ((TotalRows % displayPerPage) == 0) { 
      TotalPage = (TotalRows/displayPerPage); 
     } else { 
      TotalPage = (TotalRows/displayPerPage) + 1; // 7 
      TotalPage = (int) TotalPage; // 7 
     } 

     totalpage.setText("Page[" + TotalPage); 

     int indexRowEnd = displayPerPage * currentPage; // 5 
     if (indexRowEnd > TotalRows) { 
      indexRowEnd = TotalRows; 
     } 

     // Disabled Button Next 
     if (currentPage >= TotalPage) { 
      btnNext.setEnabled(false); 
     } else { 
      btnNext.setEnabled(true); 
     } 

     // Disabled Button Previos 
     if (currentPage <= 1) { 
      btnPre.setEnabled(false); 
     } else { 
      btnPre.setEnabled(true); 
     } 

     // Load Data from Index 
     int RowID = 1; 
     ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, 
     HashMap<String, String> map; 

     // RowID 
     if (currentPage > 1) { 
      RowID = (displayPerPage * (currentPage - 1)) + 1; 
     } 

     for (int i = indexRowStart; i < indexRowEnd; i++) { 
      Element e = (Element) nl.item(i); 

      Element e2 = (Element) n2.item(i); 

      String date = e2.getAttribute(KEY_DATE); 
      // adding each child node to HashMap key => value 

      map = new HashMap<String, String>(); 
      map.put("RowID", String.valueOf(RowID)); 

      map.put(KEY_DATE, date); 

      String mytime = date; 
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd"); 
      Date myDate = null; 
      try { 
       myDate = dateFormat.parse(mytime); 

      } catch (ParseException t) { 
       t.printStackTrace(); 
      } catch (java.text.ParseException t) { 
       // TODO Auto-generated catch block 
       t.printStackTrace(); 
      } 

      SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd"); 
      String finalDate = timeFormat.format(myDate); 

      // System.out.println("rrrrrrrrrrrrr"+finalDate); 

      String Heading = parser.getValue(e, KEY_NAME); 

      int a = Heading.indexOf("|"); 
      String beforeSubString = Heading.substring(0, a); 
      String afterSubString = Heading.substring(a, Heading.length()) 
        .replace("|", "") + "[" + finalDate + "]"; 
      // String 
      // final1="<b>"+beforeSubString+"<b>"+"|"+afterSubString.replace("|", 
      // "|\n") 
      // .replace("|", ""); 
      // String k=Html.fromHtml(final1).toString(); 
      // 
      // Html.fromHtml(final1); 

      map.put(KEY_NAME, beforeSubString); 
      map.put(KEY_NAME1, afterSubString); 

      // adding HashList to ArrayList 
      menuItems.add(map); 
      RowID = RowID + 1; 
     } 

    return null; 
} 

protected void onPostExecute(Void result) { 


    progressDialog.dismiss(); 
    SimpleAdapter sAdap; 
    sAdap = new SimpleAdapter(circularlistparsing.this, menuItems, 
      R.layout.list_item, 
      new String[] { "RowID", KEY_NAME1, KEY_NAME }, new int[] { 
        R.id.ColRowID, R.id.ColName, R.id.textView1 }); 
    lisView1.setAdapter(sAdap); 
} 
} 
} 
+0

解釋有點太amit –

+0

使這個NflAsyncTask類或您可以複製過去,並用新的NflAsyncTask()。execute();調用此類。 –

+0

我們必須移除ShowData(),我們必須調用新的NflAsyncTask()。execute();? – user1719722

1

更改你這個代碼的代碼。

public int currentPage = 1; 
public ListView lisView1; 
static final String KEY_ITEM = "docdetails"; 
static final String KEY_NAME = "heading"; 
public Button btnNext; 
public Button btnPre; 
int TotalPage = 0; 
int ERROR_CODE; 
ArrayList<HashMap<String, String>> menuItems; 
public static String url = "paste your URL"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // listView1 
    lisView1 = (ListView) findViewById(R.id.listView1); 

    // Next 
    btnNext = (Button) findViewById(R.id.btnNext); 

    // Previous 
    btnPre = (Button) findViewById(R.id.btnPre); 
    // Perform action on click 
    btnNext.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      currentPage = currentPage + 1; 
      new Data().execute(); 
     } 
    }); 

    // Perform action on click 
    btnPre.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      currentPage = currentPage - 1; 
      new Data().execute(); 
     } 
    }); 

    new Data().execute(); 
} 

private class Data extends AsyncTask<Object, Integer, Object> { 
    private ProgressDialog progress; 

    @Override 
    protected void onPreExecute() { 

     progress = ProgressDialog.show(AndroidXMLParsingActivity.this, "", 
       "Loading..."); 

     super.onPreExecute(); 
    } 

    @Override 
    protected Object doInBackground(Object... params) { 
     // do hard work here 
     ShowData(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Object result) { 

     progress.dismiss(); 
     if (ERROR_CODE == 1) { 
      Toast.makeText(AndroidXMLParsingActivity.this, "No Xml found.", 
        Toast.LENGTH_SHORT).show(); 
     } else if (ERROR_CODE == 2) { 
      Toast.makeText(AndroidXMLParsingActivity.this, "No doc found.", 
        Toast.LENGTH_SHORT).show(); 
     } else { 
      // Disabled Button Next 
      if (currentPage >= TotalPage) { 
       btnNext.setEnabled(false); 
      } else { 
       btnNext.setEnabled(true); 
      } 

      // Disabled Button Previos 
      if (currentPage <= 1) { 
       btnPre.setEnabled(false); 
      } else { 
       btnPre.setEnabled(true); 
      } 

      SimpleAdapter sAdap; 
      sAdap = new SimpleAdapter(AndroidXMLParsingActivity.this, 
        menuItems, R.layout.list_item, new String[] { "RowID", 
          KEY_NAME }, new int[] { R.id.ColRowID, 
          R.id.ColName }); 
      lisView1.setAdapter(sAdap); 
     } 
    } 
} 

public void ShowData() { 
    XMLParser parser = new XMLParser(); 
    String xml = parser.getXmlFromUrl(url); // getting XML 
    if (xml != null) { 
     Document doc = parser.getDomElement(xml); // getting DOM element 
     if (doc != null) { 
      NodeList nl = doc.getElementsByTagName(KEY_ITEM); 

      int displayPerPage = 5; // Per Page 
      int TotalRows = nl.getLength(); 
      int indexRowStart = ((displayPerPage * currentPage) - displayPerPage); 

      if (TotalRows <= displayPerPage) { 
       TotalPage = 1; 
      } else if ((TotalRows % displayPerPage) == 0) { 
       TotalPage = (TotalRows/displayPerPage); 
      } else { 
       TotalPage = (TotalRows/displayPerPage) + 1; // 7 
       TotalPage = (int) TotalPage; // 7 
      } 
      int indexRowEnd = displayPerPage * currentPage; // 5 
      if (indexRowEnd > TotalRows) { 
       indexRowEnd = TotalRows; 
      } 

      // Load Data from Index 
      int RowID = 1; 
      menuItems = new ArrayList<HashMap<String, String>>(); 
      HashMap<String, String> map; 

      // RowID 
      if (currentPage > 1) { 
       RowID = (displayPerPage * (currentPage - 1)) + 1; 
      } 

      for (int i = indexRowStart; i < indexRowEnd; i++) { 
       Element e = (Element) nl.item(i); 
       // adding each child node to HashMap key => value 
       map = new HashMap<String, String>(); 
       map.put("RowID", String.valueOf(RowID)); 
       map.put(KEY_NAME, parser.getValue(e, KEY_NAME)); 

       // adding HashList to ArrayList 
       menuItems.add(map); 

       RowID = RowID + 1; 
      } 
     } 
     ERROR_CODE = 2; 
    } 
    ERROR_CODE = 1; 
}