我有以下代码:
public class MainActivity extends Activity {
private ListView list;
private static final String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "BOE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
String books[] = {"Mercedes", "Skoda", "Volkswagen", "Audi", "Citroen", "plik"};
BookArrayAdapter bma = new BookArrayAdapter(this, books);
bma.setNotifyOnChange(true);
list.setAdapter(bma);
if (!new File(path).exists()) {
new File(path).mkdirs();
}
for (int i=0; i<books.length; i++) {
View v = list.getAdapter().getView(i, null, null);
String name = ((TextView)v.findViewById(R.id.row_text)).getText() + "";
for (File f:new File(path).listFiles()) {
if (f.isFile()) {
Button b = (Button) v.findViewById(R.id.row_button);
if (name.equals(f.getName().substring(0, f.getName().length()-4))) {
b.setText("Open");
} else {
b.setText("Download");
}
bma.notifyDataSetInvalidated();
bma.notifyDataSetChanged();
Log.d("AC", b.getText().toString());
}
}
}
}
public class BookArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public BookArrayAdapter(Context context, String[] values) {
super(context, R.layout.row, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.row, parent, false);
final TextView textView = (TextView) rowView.findViewById(R.id.row_text);
textView.setText(values[position]);
rowView.findViewById(R.id.row_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((Button)v).getText().toString().equals("Open")) {
//open
} else {
//Download
}
}
});
return rowView;
}
}
}
我在onCreate中的for循环中遇到了问题。此日志显示正确的下载/打开文本,取决于文件系统中的文件。但是此列表视图的图形表示不会改变。这是我的layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
<RelativeLayout
android:id="@+id/main_linear_blackout"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="match_parent"
android:background="#CC000000" >
<ProgressBar
android:id="@+id/main_progress_startup"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</RelativeLayout>
和我的row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/row_button"
android:orientation="vertical" >
<TextView
android:id="@+id/row_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/row_progress"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="invisible" />
</LinearLayout>
<Button
android:id="@+id/row_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/row_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/row_button"
android:layout_alignBottom="@+id/row_button"
android:layout_alignLeft="@+id/row_button"
android:layout_alignRight="@+id/row_button"
android:layout_alignTop="@+id/row_button"
android:gravity="center"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible" />
</RelativeLayout>
如何更新?有什么问题?
修改
我将编辑单行移动到我的适配器类上的getView(...)覆盖方法。我真的不知道为什么我的第一个想法是错的,但现在它有效。