对于Android应用程序...我在调用自定义ListActivity的Activity上有一个按钮。这个ListActivity有两行文本和一个复选框。当被调用时,ListActivity在设备上打开一个XML文件(local.xml
)。该XML文件包含Web上的目标XML文件列表。如果设备上存在该文件,则选中ListActivity上的复选框,否则不会。Android应用程序 - 如何更新我的ListItem?
当按下ListItem时,它会检查目标文件是否存在于设备上 - 如果存在,它将显示一个对话框,询问是否要覆盖。如果文件不存在,或者他们选择覆盖,当进入互联网并抓取一组文件时(目标XML文件包含要收集的JPeg列表),会显示进度对话框。
下载完JPegs之后,我更改了进度信息以显示是否下载了所有的JPegs。它睡了几秒钟,然后消失。
以上所有作品。
我的问题是:
完成后,如何设置与按项目相关联的复选框,根据是否所有的JPEG文件的下载或不?
我真的很喜欢三态指标,而不是复选框,它是二进制的,除非我可以将颜色更改为黄色。是否有更好的小部件,我应该在这里使用?
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>
谢谢!
我看过类似的问题,没有运气。我能够更新列表本身,但我无法弄清楚如何/在哪里触发适配器更新,使用'notifyDatasetChange()'。 – Chuck
好吧,[链接](http://stackoverflow.com/questions/11807029/change-value-of-listitem-onitemclick?rq=1)的例子告诉我如何创建ListAdapter,以便它在整个班级范围内。 当我把'.notifyDatasetChanged()'函数调用到后台线程时,我得到错误:'只有创建视图heirarchy的原始线程才能触及其视图'。当我把这个调用移动到'progresHandler'时,我没有收到错误,但它并没有更新列表中的复选框。不知道现在离我而去......有什么想法? – Chuck
好吧,有没有人回答这个问题?与我的声誉有关吗? – Chuck