Android app - how do I update my ListItem?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 15:21:52

问题


For an Android app...I have a button on an Activity that calls a custom ListActivity. This ListActivity has two lines of text and a checkbox. When invoked, the ListActivity opens up an XML file on the device (local.xml) . This XML file contains a list of target XML files on the web. If the file exists on the device, the checkbox on the ListActivity is checked, otherwise it isn't.

When the ListItem is pressed, it checks to see if the target file exists on the device-if it does, it displays a dialog box asking if they want to overwrite. If the file doesn't exist, or if they chose to overwrite, a progress dialog is displayed as it goes to the internet and grabs a set of files (the target XML file contains a list of JPegs to gather).

After downloading the JPegs, I change the message on the progress to show whether all the JPegs downloaded or not. It sleeps for a few seconds, then disapears.

All of the above works.

My questions are:

  1. After completion, how do I set the checkbox associated with the pressed item, based on whether all of the JPegs downloaded or not?

  2. I'd really like a tri-state indicator instead of a checkbox, which is binary, unless I could change the color to yellow. Is there a better widget I should be using here?

Relvant code follows (let me know if you need to see more)

Initial Activity:

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);
    }
}

Custom List Activity:

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);
        }
}

List Item Layout 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>

Thanks!!!


回答1:


OK, I got it. the problem was where I placed the call to dismiss the dialog box. It ended up inside a catch statement and was never executing. In fixing this, I also parameterized my calls to the handler, which made things clearer.

Wheh! :-)



来源:https://stackoverflow.com/questions/12430801/android-app-how-do-i-update-my-listitem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!