如何在android中的列表视图中显示所有项目 [英] How to show all items on list view in android

查看:148
本文介绍了如何在android中的列表视图中显示所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序。在此应用程序中,通过警告对话框在SD卡上创建的文件夹将显示在列表视图中。为了显示我使用Sheared Preference的所有文件夹。

我面临一个问题。当我在列表视图上添加两个或多个文件夹并返回活动时,会出现此问题。当我返回此活动时,只有列表视图上可见的最后一个文件夹。其他文件夹在列表视图中消失。请帮我解决这个问题。



代码如下,

I am developing android application. In this application, the folder created on sdcard through alert dialog will displayed on list view. For showing the all folders I uses Sheared Preference.
I facing one problem. This problem is when I add two or more folders on list view, and get back to the activity. When I return back this activity only last folder visible on list view. Other folders disappeared on list view. Please help me for solving this problem.

The code is given below,

public class FolderFragment extends DialogFragment {

    View view;
    Button createfolder_btn;
    ListView folderlist;
    private static ArrayList<String> arrayList;
    List<Message> sms;
     ArrayAdapter<String> adapter;
    File file;

   @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.fragment_folder, container, false);

        createfolder_btn = (Button) view.findViewById(R.id.btnfolder);
        folderlist = (ListView) view.findViewById(R.id.folder_listview);
        arrayList = new ArrayList<>();
    
         createfolder_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCreateDialog(savedInstanceState);
            }
        });

        adapter = new ArrayAdapter<>(getActivity(), R.layout.support_simple_spinner_dropdown_item, arrayList);
        folderlist.setAdapter(adapter);
        LoadPreferences();

        folderlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), FolderActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

             return view;

    }

  @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Create Folder");
        final EditText name_et = new EditText(getActivity());
        name_et.setHint("Enter Folder Name");
        builder.setView(name_et);
        builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                String name = name_et.getText().toString();
                String extSdcard = Environment.getExternalStorageDirectory().toString();
                file = new File(extSdcard, name);
                file.mkdir();

                 arrayList.add(file.getName());
                adapter.notifyDataSetChanged();
                saveStringToPreferences("label",file.getName());
            }
        });

        builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });

       AlertDialog dialog = builder.create();
        dialog.show();
        return dialog;
    }

    private void saveStringToPreferences(String key,String str){
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, str);
        editor.apply();
    }

    protected void LoadPreferences(){
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String previousText = preferences.getString("label", "");
        arrayList.add(previousText);
        adapter.notifyDataSetChanged();
    }





我的尝试:



我试过以下代码,



What I have tried:

I tried following code,

protected void LoadPreferences(){
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String previousText = preferences.getString("label", "");
        for(int i=0;i<previousText.length();i++){
        arrayList.add(previousText);
        adapter.notifyDataSetChanged();
     }
    }





但是,此代码不起作用。请帮我解决这个问题。



But, this code does not works. Please help me solving this problem.

推荐答案

这与如何在android中的列表视图上永久保存列表项 [ ^ ]。你是什​​么意思这段代码不起作用



正如我昨天解释的那样你需要花时间调试你的调试器有关代码运行时出现的情况以及出错的信息。没有mor信息就无法提供帮助。



只是转储代码并期望其他人为你调试它是没有用的。
This looks like the same issue as How to save list items permanently on list view in android[^]. And what do you mean by "this code does not work"?

As I explained yesterday you need to spend time with your debugger collecting information about what happens when your code runs and where things go wrong. Without mor information it is impossible to offer help.

It is no use just dumping your code and expecting other people to debug it for you.


这篇关于如何在android中的列表视图中显示所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆