Nice programing

목록보기 콘텐츠를 지우시겠습니까?

nicepro 2020. 10. 22. 22:44
반응형

목록보기 콘텐츠를 지우시겠습니까?


에 약간의 문제가 ListView있습니다. ListView사용자 지정 어댑터가 있다는 것을 알고 콘텐츠를 지우려면 어떻게합니까 ?

edit-사용자 정의 어댑터 클래스 BaseAdapter는 다음과 같이 확장됩니다 .

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater = null;

    public MyAdapter(Activity a, String[] str) {
        activity = a;
        data = str;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder {
        public TextView text;
    }

    @Override
    public int getCount() {
        return data.length;
    }

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

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

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        View v = view;
        ViewHolder holder;
        if (v == null) {
            v = inflater.inflate(R.layout.rowa, null);
            holder = new ViewHolder();
            holder.text= v.findViewById(R.id.dexter);
            v.setTag(holder);
        } else {
            holder = v.getTag();
        }

        holder.text.setText(data[position]);

        return v;
    }

}

간단히 쓰기

listView.setAdapter(null);

목록이나 배열을 어댑터에 전달한 것 같습니다. 이 추가 된 컬렉션의 인스턴스를 유지하면 다음을 수행 할 수 있습니다.

collection.clear();
listview.getAdapter().notifyDataSetChanged();

컬렉션을 사용 하여 어댑터를 인스턴스화 하고 동일한 인스턴스 인 경우에만 작동 합니다.

또한 확장 한 어댑터에 따라이 작업을 수행하지 못할 수 있습니다. SimpleAdapter 는 정적 데이터에 사용되므로 생성 후 업데이트 할 수 없습니다.

PS. not all Adapters have a clear() method. ArrayAdapter does, but ListAdapter or SimpleAdapter don't


It's simple .First you should clear your collection and after clear list like this code :

 yourCollection.clear();
 setListAdapter(null);

As of Android versions M and N, following works for me and would be the correct approach. Emptying the ListView or setting the Adapter to null is not the right approach and would lead to null pointer issue, invalid ListView and/or crash of the app.

Simply do:

    mList.clear();
    mAdapter.notifyDataSetChanged();

i.e. first you clear the list altogether, and then let the adapter know about this change. Android will take care of correctly updating the UI with an empty list. In my case, my list is an ArrayList.

In case you are doing this operation from a different thread, run this code on the UI thread:

    runOnUiThread(mRunnable);

Where mRunnable would be:

    Runnable mRunnable = new Runnable() {
        public void run() {
            mList.clear();
            mAdapter.notifyDataSetChanged();
        }
    };;

Simple its works me:)

YourArrayList_Object.clear();

Remove your items from your custom adapter and call notifyDataSetChanged().


There is a solution for the duplicate entry in listview. You have to declare the onBackPress()-method on your activity and write down the highlight code given below:

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

    attendence_webdata.clear(); list.setAdapter(null);
    --------------------------------------------------

    **
 }

Just put the code ListView.Items.Clear(); on your method


You need to call both clear() from ArrayAdapter and notifyDataSetChanged() both.

Below is link click


Call clear() method from your custom adapter .

참고URL : https://stackoverflow.com/questions/3802304/clear-listview-content

반응형