0

在这里我有一个Fragment,我使用这个代码并且一切正常,如果有一个新文件,我想要做的是更新我的显示列表,你们能提供任何建议或提示吗?

代码:

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);
            }
        });
    }
}
}

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

4

1 回答 1

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(); 
于 2013-10-03T06:08:42.680 回答