【问题标题】:Android app - how do I update my ListItem?Android 应用程序 - 如何更新我的 ListItem?
【发布时间】:2012-09-14 19:26:30
【问题描述】:

对于 Android 应用程序...我在调用自定义 ListActivity 的 Activity 上有一个按钮。这个 ListActivity 有两行文本和一个复选框。调用时,ListActivity 在设备上打开一个 XML 文件 (local.xml)。此 XML 文件包含 Web 上的目标 XML 文件列表。如果设备上存在该文件,则选中 ListActivity 上的复选框,否则不选中。

当 ListItem 被按下时,它会检查目标文件是否存在于设备上——如果存在,它会显示一个对话框询问是否要覆盖。如果文件不存在,或者如果他们选择覆盖,则会在进入 Internet 并抓取一组文件时显示一个进度对话框(目标 XML 文件包含要收集的 JPegs 列表)。

下载 JPegs 后,我更改进度消息以显示是否所有 JPegs 都已下载。它会休眠几秒钟,然后消失。

以上所有工作。

我的问题是:

  1. 完成后,如何设置与按下的项目相关的复选框,基于是否所有的 JPegs 都下载了?

  2. 我真的想要一个三态指示器而不是一个复选框,它是二进制的,除非我可以将颜色更改为黄色。我应该在这里使用更好的小部件吗?

相关代码如下(如果您需要查看更多信息,请告诉我)

初始活动:

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>

谢谢!!!

【问题讨论】:

  • 我看过类似的问题,但没有运气。我可以更新列表本身,但我不知道如何/在哪里触发适配器更新,使用notifyDatasetChange()
  • 好的,link 的示例向我展示了如何创建ListAdapter,以便它在整个班级的范围内。当我将对.notifyDatasetChanged() 函数的调用放入后台线程时,出现错误:Only the original thread that created a view heirarchy can touch its views。当我将此调用移至progresHandler 时,我没有收到错误消息,但它也没有更新列表上的复选框。不知道现在把我留在哪里了……有什么想法吗?
  • 好的,没人回答这个问题是有原因的吗?跟我的名声有关系吗?

标签: android listitem


【解决方案1】:

好的,我明白了。问题出在我拨打电话以关闭对话框的位置。它最终在一个 catch 语句中并且从未执行。在解决这个问题时,我还将对处理程序的调用参数化,这让事情变得更加清晰。

哇! :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    相关资源
    最近更新 更多