0

我有一个ListView设置,我动态添加项目,每个项目都有不同的数据。其中一条数据是时间/日期,它显示TextView在我的自定义列表项布局中。在我的 onResume() 方法中,我Fragment想删除显示日期早于当前日期的项目。

List 项布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pi_rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/pi_tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/pi_tv_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/pi_tv_title"
        android:textSize="24sp" />


</RelativeLayout>

我必须解决这个问题的想法是创建一个循环,该循环遍历所有项目,检查每个项目的日期 TextView 的内容。然后,如果日期早于当前日期,它将删除该项目。

我目前能够根据那里的位置删除项目,但是如果它们具有特定的内容,我无法弄清楚如何删除单个列表项

这是我到目前为止所拥有的:

@Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        int items = DataModel.getInstance().getPendingItemList().size(); // Number of items in the list

        if (items > 0) {
            for (int i = 1; i <= items; i++) { // This loops through all my items. Do I want to use position instead?
                Log.i("for loop", "" + i);

// This doesn't work because it tries to get the content for all the items not just one.

                TextView tv_date = (TextView) getView()
                        .findViewById(R.id.pi_tv_date);

                CharSequence c = tv_date.getText(); // Causes force close

                                 // Here I will have my if(date shown != current date){
//deleteItem
//}

            }

        }

    }

编辑:这是我的适配器代码:

private class PendingAdapter extends BaseAdapter { // This happens whenever
                                                    // onCreate is called.

    private List<Map<String, Object>> mPendingItemList;

    public PendingAdapter() {
        mPendingItemList = DataModel.getInstance().getPendingItemList();
    }

    @Override
    public int getCount() {
        return mPendingItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return mPendingItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = LayoutInflater.from(getActivity()).inflate(
                    R.layout.pending_item, null);
            // Log.i("convertView", "was null");
        }

        TextView tv_title = (TextView) convertView
                .findViewById(R.id.pi_tv_title);

        TextView tv_date = (TextView) convertView
                .findViewById(R.id.pi_tv_date);

        HashMap<String, String> itemDataHashMap = (HashMap<String, String>) getItem(position);

        tv_title.setText(itemDataHashMap.get("planet"));
        tv_date.setText(itemDataHashMap.get("date"));

        return convertView;
    }
}

编辑2:

根据Ali AlNoaimi的回答,我在我的适配器中添加了这个:

public void refreshMyAdapter() {

            if(mPendingItemList.size() > 0) {
            List<Map<String, Object>> newPendingList = mPendingItemList;

            for (int i = 0; i < mPendingItemList.size(); i++) {
                Map<String, Object> item = mPendingItemList.get(i);

                TextView tv_title = (TextView) histView
                        .findViewById(R.id.pi_tv_title);

                String s_title;

                s_title = tv_title.getText().toString();

                 Log.i("s_title", s_title);

                if(s_title == "foo") {

                    newPendingList.remove(item);
                }


            }

            mPendingItemList = newPendingList;
        }

        }

这对onResume()

@Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        final Handler handler = new Handler(); new Thread(new Runnable() {

            @Override
            public void run() {
                while(true) {

                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            simpleAdpt.refreshMyAdapter();
                            simpleAdpt.notifyDataSetChanged();

                        }

                    });



                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }).start();

这仍然不起作用。

最终编辑: Ali AlNoaimi的答案确实有效,我只是错误地比较了字符串。

我希望这是有道理的!谢谢

4

4 回答 4

1

I'm not sure you understand how ListView works. A ListView is the visual representation of the data in your adapter, so you should never try to edit, add or delete views of a ListView directly, but instead edit the data and then call notifyDataSetChanged() on your adapter.

The ListView will then check for changes in your adapter's data and refresh the views accordingly.

For your specific problem, it means that you should work directly with mPendingItemList, find the items that have a specific content, remove them and then call notifyDataSetChanged().

于 2013-08-25T09:56:46.063 回答
0

to have a coherent implementation, keep data processing on the DataModel. add to DataModel class this method:

public void clearObsoleteItems(){
    //TODO: add you implementation to clear the obsolete items from the List of items holded by the singleton class. 

}

add on your adapter class PendingAdapter a setter for items list:

public void setPendingItemList(List<Map<String, Object>> mPendingItemList) {
this.mPendingItemList = mPendingItemList;
notifyDataSetChanged();

}

from the activity onResume call:

DataModel.getInstance().clearObsoleteItems();

mAdapter.setPendingItemList(DataModel.getInstance().getPendingItemList());
于 2013-08-25T14:24:07.097 回答
0

将此方法添加到您的适配器:

public void refreshMyAdapter() {

    private List<Map<String, Object>> newPendingList;

    for (int i = 0; i < mPendingItemList.size(); i++) {
        Map<String, Object> item = mPendingItemList.get(i);

        if(it matches) {
            newPendingList.add(item);
        }


    }

    mPendingItemList = newPendingList;
}

在你的主要活动中,片段

    final Handler handler = new Handler();
    new Thread(new Runnable() {

        @Override
        public void run() {
            while(true) {

                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        yourPendingAdapter.refreshMyAdapter();
                        yourPendingAdapter.notifyDataSetChanged();

                    }

                });



                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }).start();
于 2013-08-25T10:05:41.317 回答
0

最好的方法是删除创建适配器时使用的 ArrayList 中的项目,然后调用notifyDataSetChanged();adabter 来更新列表视图。

于 2013-08-25T12:14:51.757 回答