问题:
在滚动列表视图时,有时即使光标没有变化,也不会显示某些列表项的字段,即列表项呈现但不显示textview3 / statusIcon。
当我点击某个项目并再次查询内容提供程序时,会出现同样的问题。然后在Customadapter中更改获取的光标。
如果需要更多信息,请告知我们,我们会更新问题。
情景:
使用activity
方法content provider
查询数据库的onResume
。
然后将结果cursor
传递给CustomCursorAdapter
。然后将此适配器传递给listview。列表更像是文件目录列表。对于某些项目,单击时应再次查询数据库并更改更新的光标以再次呈现UI。
“列出”项目在大多数时间内将数据呈现为已编程。
listeitem.xml如下: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView3"
android:layout_below="@+id/TextView3"
android:text="fileSize" />
<ImageView
android:id="@+id/itemIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:layout_marginTop="25dp"
android:contentDescription="file Icon"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/itemIcon"
android:layout_marginLeft="37dp"
android:layout_toRightOf="@+id/itemIcon"
android:text="FileName"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/statusIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/TextView3"
android:layout_marginRight="21dp"
android:src="@drawable/downloaded" />
</RelativeLayout>
调用以下方法从DB获取Cursor。另外注释,目前这个方法直接在UI线程上调用。
public Cursor getlistFromDB() {
ContentResolver cr = getContentResolver();
Cursor dataCursor = cr.query(CONTENT_URI, projections,
selection, selectionArguments, null);
return dataCursor ;
}
CustomListDBAdapter.java
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListDBAdapter extends SimpleCursorAdapter {
private final String tag = "CustomListDBAdapter";
private int layout;
public CustomListDBAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
Log.d(tag, "constructor 1");
this.layout = layout;
}
public CustomListDBAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
System.out.println(" cursor count ");
this.layout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d(tag, " in newView");
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
Log.d(tag, " in bindView");
//get the column numbers using column name
int column1 = c.getColumnIndex(TableMetaData.column1);
int column2 = c.getColumnIndex(TableMetaData.column2);
int column3 = c.getColumnIndex(TableMetaData.column3);
int column4 = c.getInt(c.getColumnIndex(TableMetaData.column4));
//get the column data using the column number
String column1_Text = c.getString(column1);
int column2_Type = c.getInt(column2);
long column3_data = c.getLong(column3);
//Setting data to the UI components
TextView column1_TextTxt = (TextView)v.findViewById(R.id.TextView1);
column1_TextTxt.setText(column1_Text);
ImageView itemIcon = (ImageView)v.findViewById(R.id.itemIcon);
//getImageResourceID is a method to get correct image according to column2_Type
itemIcon.setImageResource(getImageResourceID(column2_Type));
TextView textView3 = (TextView)v.findViewById(R.id.TextView3);
ImageView downloadStatusIcon = (ImageView)v.findViewById(R.id.statusIcon);
switch(column2_Type){
case FileListTableMetaData.filetype_dir:
textView3.setVisibility(View.GONE);
downloadStatusIcon.setVisibility(View.GONE);
break;
default:
textView3.setText("Test");
downloadStatusIcon.setImageResource(R.drawable.image1);
}
}
}