列出视图'调用notifyDataSetChanged()时,ArrayAdapter列表更新两次

时间:2014-09-20 04:10:23

标签: android android-arrayadapter

我尝试调用notifyDataSetChange()来重绘ArrayAdapter中的数据,但是ArrayAdapter中的整个数据将被重绘两次。

这是activity_search.xml:

  <?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="match_parent"
        android:orientation="vertical">

        <view
            class = "com.search.widget.ClearableEditText"
            android:id = "@+id/searchText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Search Tours"
            android:maxLines="1"
            android:singleLine="true"
            />


        <ListView
                android:id="@+id/result_list"
                android:cacheColorHint="#00000000"
                android:scrollbars="none"
                android:fadingEdge="vertical"
                android:soundEffectsEnabled="true"
                android:divider="@color/Orange"
                android:dividerHeight="1px"
                android:padding="0dip"
                android:smoothScrollbar="true"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="false"
                android:layout_marginTop="5dip"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_marginBottom="5dip"
                android:layout_weight="1"/>

            <ProgressBar
            android:id="@+id/loadingResult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone" />

    </LinearLayout>

这是每行的布局是listView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rows"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:id="@+id/Tours"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text=""
        android:gravity="center" 
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/Authors"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:gravity="center"
        android:text=""
        android:layout_weight="1"/>

</LinearLayout>

以下是我初始化ListView和ArrayAdapter的方法:

 resultiew = (ListView) findViewById(R.id.result_list);
        this.myadp = new myAdapter(this, android.R.layout.simple_list_item_1,
                results);
        resultiew.setAdapter(myadp);

        // render result
        resultChangeListener();

resultChangeListerner()是一个线程,用于在更新应该绘制的数据时调用notifyDataSetChange(),这里是代码:

private void resultChangeListener() {
    new Thread() {
        public void run() {
            while (true) {
                try {
                    if (SearchActivity.listModified == true) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                SearchActivity.listModified = false;

                                resetInfos();
                                Log.e("change search location result",
                                        "there");
                                System.out.println("adapter size:"
                                        + myadp.list.size());
                                myadp.notifyDataSetChanged();
                                System.out.println("listview count: "
                                        + resultiew.getCount());
                            }
                        });
                    }
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
}

SearchActivity.listModified指示数据的更新位置。

这是resetInfos()的代码,它清除结果集并向其添加内容。

private void resetInfos() {
    results.clear();
    toursTextViews.clear();
    authorsTextViews.clear();
    results.addAll(labels);
}

以下是我的ArrayAdapter:

private class myAdapter extends ArrayAdapter<String> {
        public myAdapter(Context context, int resource, List<String> objects) {
            super(context, resource, objects);
            this.list = objects;
        }

        View row;

        private List<String> list;

        public View getView(int position, View convertView, ViewGroup parent) {
            {
                String rowMetaContent[] = this.list.get(position).split(
                        SearchActivity.delimiter);

                LayoutInflater inflater = getLayoutInflater();

                row = inflater.inflate(R.layout.search_results_row_layout,
                        parent, false);

                if (position == 0) {
                    TextView headerTours = (TextView) row
                            .findViewById(R.id.Tours);
                    TextView headerAuthors = (TextView) row
                            .findViewById(R.id.Authors);
                    setHeaderStyle(headerTours, "Tours");
                    setHeaderStyle(headerAuthors, "Authors");

                }

                else {
                    try {

                        TextView headerTours = (TextView) row
                                .findViewById(R.id.Tours);
                        TextView headerAuthors = (TextView) row
                                .findViewById(R.id.Authors);

                        switch (rowMetaContent.length) {
                        case 7:
                            headerTours.setPadding(4, 16, 1, 16);
                            headerTours.setTextColor(Color.BLACK);

                            this.setTextStyle(headerTours, rowMetaContent[0]);
                            keyWordSearchTours.add(rowMetaContent[2]);
                            toursTextViews.add(headerTours);
                            headerTours.setOnClickListener(onclicklistener);

                            headerAuthors.setPadding(4, 16, 1, 16);
                            headerAuthors.setTextColor(Color.BLACK);
                            authorsTextViews.add(headerAuthors);
                            headerAuthors.setOnClickListener(onclicklistener);

                            this.setTextStyle(headerAuthors, rowMetaContent[4]);
                            break;
                        case 4:
                            headerTours.setPadding(4, 16, 1, 16);
                            headerTours.setTextColor(Color.BLACK);
                            this.setTextStyle(headerTours, rowMetaContent[0]);
                            keyWordSearchTours.add(rowMetaContent[2]);
                            toursTextViews.add(headerTours);
                            headerTours.setOnClickListener(onclicklistener);

                            headerAuthors.setPadding(4, 16, 1, 16);
                            headerAuthors.setTextColor(Color.BLACK);
                            break;

                        default:
                            headerTours.setPadding(4, 16, 1, 16);
                            headerTours.setTextColor(Color.BLACK);
                            // this.setTextStyle(headerTours, "");
                            keyWordSearchTours.add("");

                            headerAuthors.setPadding(4, 16, 1, 16);
                            headerAuthors.setTextColor(Color.BLACK);
                            this.setTextStyle(headerAuthors, rowMetaContent[0]);
                            authorsTextViews.add(headerAuthors);
                            headerAuthors.setOnClickListener(onclicklistener);
                            break;

                        }

                         System.out.println(this.list.get(position));
                         System.out.println(list.size());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
                return row;
            }
        }

        public void setHeaderStyle(TextView textView, String header) {
            textView.setBackgroundResource(R.layout.customshape_header);
            textView.setPadding(4, 2, 1, 1);
            row.setEnabled(false);
            textView.setEnabled(false);

            textView.setTextColor(Color.BLACK);
            textView.setText(header);

        }

        public void setTextStyle(TextView textView, String result) {
            try {

                // replace all * in result
                String partString[] = result.split("\\$");
                String newResult = partString[0];
                int start = Integer.parseInt(partString[1]);
                int end = Integer.parseInt(partString[2]);
                final SpannableStringBuilder sb = new SpannableStringBuilder(
                        newResult);

                final ForegroundColorSpan fcs = new ForegroundColorSpan(
                        Color.rgb(255, 97, 0));
                final StyleSpan bss = new StyleSpan(
                        android.graphics.Typeface.BOLD);

                sb.setSpan(fcs, start, end + 1,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                sb.setSpan(bss, start, end + 1,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                // make them also bold
                textView.setText(sb);
            } catch (Exception e) {
                System.out.println("error:  " + result);
                Log.d("exception error", e.toString());
            }

        }
    }

以下是LogCat的输出,ArrayAdapter中的列表大小为3,但getView()被调用了6次。

09-20 15:47:35.530: I/System.out(4095): adapter size:3
09-21 15:47:35.530: I/System.out(4095): Position: 1
09-21 15:47:35.530: I/System.out(4095): josh's tour1$0$3###@@@Content###@@@josh-1410695945754###@@@Tours###@@@josh$0$3###@@@Content###@@@Authors
09-21 15:47:35.530: I/System.out(4095): Position: 2
09-21 15:47:35.530: I/System.out(4095): joshua's tour$0$3###@@@Content###@@@joshua's_tour-1409049012110###@@@Tours###@@@joshua$0$3###@@@Content###@@@Authors
09-21 15:47:35.540: I/System.out(4095): Position: 3
09-21 15:47:35.540: I/System.out(4095): joshua's tour$0$3###@@@Content###@@@joshua's_tour-1408968854379###@@@Tours###@@@joshua$0$3###@@@Content###@@@Authors
09-21 15:47:35.550: I/System.out(4095): Position: 1
09-21 15:47:35.550: I/System.out(4095): josh's tour1$0$3###@@@Content###@@@josh-1410695945754###@@@Tours###@@@josh$0$3###@@@Content###@@@Authors
09-21 15:47:35.560: I/System.out(4095): Position: 2
09-21 15:47:35.560: I/System.out(4095): joshua's tour$0$3###@@@Content###@@@joshua's_tour-1409049012110###@@@Tours###@@@joshua$0$3###@@@Content###@@@Authors
09-21 15:47:35.570: I/System.out(4095): Position: 3
09-21 15:47:35.570: I/System.out(4095): joshua's tour$0$3###@@@Content###@@@joshua's_tour-1408968854379###@@@Tours###@@@joshua$0$3###@@@Content###@@@Authors

我试图在onCreate()中对这些代码中的所有代码进行注释:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setTitle("");
        getActionBar().hide();
        setContentView(R.layout.activity_search);

        ListView resultiew = (ListView) findViewById(R.id.result_list);
        ArrayList<String> results = new ArrayList<String>();
        this.myadp = new myAdapter(this, android.R.layout.simple_list_item_1, results);
        resultiew.setAdapter(myadp);
        results.add("hello"); 
}

我得到了这个logCat输出:

09-21 16:26:42.490: I/System.out(8084): Position: 0
09-21 16:26:42.490: I/System.out(8084): hello
09-21 16:26:42.490: I/System.out(8084): 1
09-21 16:26:42.530: I/System.out(8084): Position: 0
09-21 16:26:42.530: I/System.out(8084): hello
09-21 16:26:42.530: I/System.out(8084): 1
09-21 16:26:42.560: I/System.out(8084): Position: 0
09-21 16:26:42.560: I/System.out(8084): hello
09-21 16:26:42.560: I/System.out(8084): 1

1 个答案:

答案 0 :(得分:-1)

设置适配器时,将绘制数据。再次使用notyfyDataSetChanged()。