2012-09-14 21 views
0

對於Android應用程序...我在調用自定義ListActivity的Activity上有一個按鈕。這個ListActivity有兩行文本和一個複選框。當被調用時,ListActivity在設備上打開一個XML文件(local.xml)。該XML文件包含Web上的目標XML文件列表。如果設備上存在該文件,則選中ListActivity上的複選框,否則不會。Android應用程序 - 如何更新我的ListItem?

當按下ListItem時,它會檢查目標文件是否存在於設備上 - 如果存在,它將顯示一個對話框,詢問是否要覆蓋。如果文件不存在,或者他們選擇覆蓋,當進入互聯網並抓取一組文件時(目標XML文件包含要收集的JPeg列表),會顯示進度對話框。

下載完JPegs之後,我更改了進度信息以顯示是否下載了所有的JPegs。它睡了幾秒鐘,然後消失。

以上所有作品。

我的問題是:

  1. 完成後,如何設置與按項目相關聯的複選框,根據是否所有的JPEG文件的下載或不?

  2. 我真的很喜歡三態指標,而不是複選框,它是二進制的,除非我可以將顏色更改爲黃色。是否有更好的小部件,我應該在這裏使用?

Relvant代碼如下(讓我知道如果您需要查看更多)

初始活性:

public class RRS_Preferences extends Activity { 
    onCreate(yadda, yadda) { 
} 

public void Button_Listener(View view) { 
    /* open up the ListView Activity */ 
    Intent myIntent = new Intent(); 
    myIntent.setClassName("com.sinisterlabs.mypackage", "com.sinisterlabs.mypackage.Download_List"); 
    startActivity(myIntent); 
    } 
} 

自定義列表活動:

public class Download_List extends ListActivity { 
    List<Location>loc_list = new ArrayList<Location>(); 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new RRSList_ArrayAdapter(this)); 
     selection = (TextView)findViewById(R.id.tableRow1); 

     /* Open the "local.xml" file, pull from it the list of files that need to go 
     onto the ListActivity. For each file, I add it to the List. */ 
     loc_list.add(new Location(stringLocalFilename, stringURL, booleanIsPresent)); 
    } 

    protected void onListItemClick(final ListView parent, final View v, int position, long href) { 
     if (fileLocalFile.exists) { 
      subDownloadJPegs(fileLocalFile); 

     } else { // Ask to download or not? 
      AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); 
      alertBuilder.setMessage("Are you sure you want to OverWrite this file and all of its image files?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
         subDownloadJPegs(fileLocalFile); 
         } 
        }); 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
         Toast.makeText(getApplicationContext(), "OverWrite Operation Cancelled...", Toast.LENGTH_LONG).show(); 
         } 
        }); 
      AlertDialog alert = alertBuilder.create(); 
      alert.show(); 
     } 
    } 

    private void subDownloadJPegs(fileLocalFile) { 
     progDialog = new ProgressDialog(this); 
     progDialog.setCancelable(true); 
     progDialog.setMessage("Downloading files for " + fileLocalFile.toString() + "..."); 
     progDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progDialog.setProgress(0); 
     /* open up this file and count the number of JPegs to be downloaded */ 
     progDialog.setMax(intMax); 
     progDialog.setMessage("Downloading Sign Files for " + RuleSetName + "..."); 
     progDialog.show(); 

     /* background thread to update progress bar */ 
     Thread background = new Thread (new Runnable() { 
     @Overide 
     public void run() { 
      /* Inside a loop, download each file, increment the progress bar as we do */ 
      progressHandler.sendMessage(progressHandler.obtainMessage()); 
     } 
     background.start(); 
    } 

    Handler progressHandler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      progDialog.incrementProgressBy(1); 
     } 
} 

列表項目佈局XML :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:clickable="false" 
     android:focusable="false" 
     android:gravity="center" /> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView android:id="@+id/text1" 
      android:layout_width="fill_parent" 
      android:layout_height="20dp" 
      android:textSize="18dp"></TextView> 

     <TextView android:id="@+id/text2" 
      android:layout_width="fill_parent" 
      android:layout_height="15dp" 
      android:textSize="13dp"></TextView> 

    </LinearLayout> 
</LinearLayout> 

謝謝!

+0

我看過類似的問題,沒有運氣。我能夠更新列表本身,但我無法弄清楚如何/在哪裏觸發適配器更新,使用'notifyDatasetChange()'。 – Chuck

+0

好吧,[鏈接](http://stackoverflow.com/questions/11807029/change-value-of-listitem-onitemclick?rq=1)的例子告訴我如何創建ListAdapter,以便它在整個班級範圍內。 當我把'.notifyDatasetChanged()'函數調用到後臺線程時,我得到錯誤:'只有創建視圖heirarchy的原始線程才能觸及其視圖'。當我把這個調用移動到'progresHandler'時,我沒有收到錯誤,但它並沒有更新列表中的複選框。不知道現在離我而去......有什麼想法? – Chuck

+0

好吧,有沒有人回答這個問題?與我的聲譽有關嗎? – Chuck

回答

0

好的,我明白了。問題出在我撥打電話解除對話框的位置。它結束了一個catch語句,並且從未執行。在解決這個問題時,我還將參數化爲處理程序的參數,這使得事情變得更加清晰。

呃! :-)

相關問題