我是一个尝试我的第一个项目(API 级别 10)的 android 新手。我正在我的 ListActivity 中下载 json 数据,将其填充到本地 sqlite db 并使用自定义 CursorAdapter 显示它。我现在的布局中只有 TextViews,但会改变它,因此自定义 CursorAdapter。一切似乎都正常,数据库已填充并显示数据,但在滚动列表时它会闪烁,并且我正在覆盖文本/不干净的刷新。任何帮助,将不胜感激。我尝试在我的 ItemAdapter 中使用“ViewHolder”,但没有帮助,然后将其删除。我没有指定背景..
我的代码:
public class ItemList extends ListActivity {
private static ItemDBAdapter dao;
private static ProgressDialog dialog;
private static Cursor ItemCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Item_list);
getListView().setSmoothScrollbarEnabled(true);
dao = new ItemDBAdapter(this);
dao.open();
dialog = ProgressDialog.show(this, "Download", "Downloading");
load();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_Item_list, menu);
return true;
}
@Override
protected void onDestroy() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
super.onDestroy();
}
private class DownloadDataTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String success = "false";
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
JSONArray Items = new JSONArray(response);
dao.saveAll(Items);
} catch (Exception e) {
e.printStackTrace();
}
}
success = "true";
return success;
}
@Override
protected void onPostExecute(String result) {
// TODO: check if internet failed, not all data loaded, etc...
dialog.dismiss();
fillData();
if (result == "true") {
}
}
}
public void load() {
DownloadDataTask task = new DownloadDataTask();
task.execute(new String[] { "http://247.85.45.12:3000/Item/index.json" });
}
public void fillData() {
ItemCursor = dao.getAll();
ItemAdapter adapter = new ItemAdapter(this, ItemCursor);
setListAdapter(adapter);
startManagingCursor(ItemCursor);
}
}
public class ItemAdapter extends CursorAdapter {
private final Context context;
private int NAME, ADDRESS, PHONE, URL;
private LayoutInflater inflater;
public ItemAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
NAME = c.getColumnIndex(ItemDBAdapter.KEY_NAME);
URL = c.getColumnIndex(ItemDBAdapter.KEY_URL);
ADDRESS = c.getColumnIndex(ItemDBAdapter.KEY_ADDRESS);
PHONE = c.getColumnIndex(ItemDBAdapter.KEY_PHONE);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (view != null) {
TextView name = (TextView)view.findViewById(R.id.name);
TextView url = (TextView)view.findViewById(R.id.url);
TextView address = (TextView)view.findViewById(R.id.address);
TextView phone = (TextView)view.findViewById(R.id.phone);
name.setText(cursor.getString(NAME));
url.setText(cursor.getString(URL));
address.setText(cursor.getString(ADDRESS));
phone.setText(cursor.getString(PHONE));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.Item_item, null);
return v;
}
}
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
列表项布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/url"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>