滚动时Android ListView闪烁/无法正确刷新

时间:2012-11-06 19:54:45

标签: android listview

我是一个机器人新手试用我的第一个项目(API级别10)。我正在我的ListActivity中下载json数据,将其填充到本地sqlite数据库并使用自定义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>

2 个答案:

答案 0 :(得分:0)

问题解决了,我已将ListView宽度和线性布局宽度更改为“match_parent”

 <ListView android:id="@+id/android:list"
          android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

我注意到那些有不洁刷新和覆盖文字的区域是未使用的区域。这解决了这个问题。根本无法在虚拟设备上看到问题,但在高分辨率星系s3上非常明显

答案 1 :(得分:-1)

尝试致电

getListView.setCacheColorHint(...);

使用项目的背景颜色,如果没有这样的颜色则为0(它将禁用缓存)。如果你根本没有背景,甚至没有窗口背景,那也可能是问题。

希望它有所帮助。

相关问题