有关刷新ArrayAdapter的任何建议或提示吗?

时间:2013-10-03 05:56:39

标签: android android-arrayadapter

这里我有一个Fragment,我使用这个代码,一切正常,我想要做的是更新我显示的列表,如果有新文件,你们可以给出任何建议或提示吗? / p>

CODE:

public class HomeFragment extends Fragment {

public static final String TITLE = "title";
private List<String> library = new ArrayList<String>();
private TextView tv;
private ListView lv;
private ArrayAdapter<String> adapter;
public static Handler handHF;
private String[] temp;
private Object UIlock = new Object();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.home_fragment, container,
            false);
    library = getLibraryList();

    if (!library.isEmpty()) {
        if (tv != null) {
            tv.setVisibility(View.GONE);
        } else {
            temp = library.toArray(new String[library.size()]);
            lv = (ListView) rootView.findViewById(R.id.library_list);
            adapter = new ArrayAdapter<String>(rootView.getContext(),
                    android.R.layout.simple_list_item_1, temp);
            lv.setAdapter(adapter);
            setListener(lv);
            tv = (TextView) rootView.findViewById(R.id.library_tv1);
            tv.setVisibility(View.GONE);
            tv = null;
        }
    } else {
        tv = (TextView) rootView.findViewById(R.id.library_tv1);
        tv.setText("No Manga found...");
    }
    return rootView;
}

@SuppressLint("HandlerLeak")
@Override
public void onResume() {
    /*
     * Fragment on pause state
     */
    super.onResume();
    handHF = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                refreshAdapter();
            }
        }
    };
}

private void setListener(ListView lv) {

    /*
     * Sets listener on listView
     */

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Intent myIntent = new Intent(view.getContext(),
                    ChapterActivity.class);
            myIntent.putExtra(TITLE, parent.getItemAtPosition(position)
                    .toString());
            startActivity(myIntent);
        }
    });

    lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(view.getContext(),
                    parent.getItemAtPosition(position).toString(),
                    Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

private final List<String> getLibraryList() {
    /*
     * Returns List<String> of library
     */
    List<String> l = new ArrayList<String>();
    File dir = new File(Constants.UNDUH);
    if (dir.exists()) {
        File[] dirs = dir.listFiles();
        for (File i : dirs) {
            l.add(i.getName());
        }
        return l;
    } else {
        return l;
    }
}

private void refreshAdapter() {
    /*
     * It will update library and
     */
    synchronized (UIlock) {
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                if (tv != null) {
                    tv.setVisibility(View.GONE);
                }
                library = getLibraryList();
                temp = library.toArray(new String[library.size()]);
                lv = (ListView) getActivity().findViewById(
                        R.id.library_list);
                adapter = new ArrayAdapter<String>(getActivity(),
                        android.R.layout.simple_list_item_1, temp);
                lv.setAdapter(adapter);
            }
        });
    }
}
}

任何建议将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

更新您正在使用

的情况下传递给ListView的字符串列表
private String[] temp;

使用 notifyDataSetChanged 方法,只需在你的适配器之后调用它,如果你的temp []递增,它会自动添加更多的项目列表。

像这样

    adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, temp);
    lv.setAdapter(adapter);
    adapter.notifyDataSetChanged();

您在temp []中添加更多数据的位置 添加额外的行

((ArrayAdapter) lv.getAdapter()).notifyDataSetChanged();