简体   繁体   English

布局不膨胀

[英]Layout not inflating

the below code is used to create a list of notes.下面的代码用于创建笔记列表。 I want to take the title of the note and add to to the dynamic layout.我想取笔记的标题并添加到动态布局中。 but the layout is not inflating.但布局并没有膨胀。

public class TakeNote extends AppCompatActivity {

List<String> titles = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_take_note);
    reader();
    LinearLayout layout = (LinearLayout) findViewById(R.id.tn);
    try {
        addlist(titles, layout);
    }
    catch(Exception e){

    }
}

this reads the file where the titles are stored...这将读取存储标题的文件...

 public void reader(){


        try {
            Scanner sc = new Scanner(openFileInput("titles.txt"));
            while(sc.hasNextLine()){
                titles.add(sc.nextLine());
            }
            sc.close();

        }
        catch(Exception e){
        }

    }

this writes the titles to a file.这会将标题写入文件。

 public void writer(String title){
    try {
        PrintStream write = new PrintStream(openFileOutput("titles.txt", MODE_PRIVATE | MODE_APPEND));
        write.println(title);

        write.close();
    }
    catch(Exception e){

    }

}
ArrayAdapter<String> adap;

private void addlist(List<String> l,LinearLayout layout){
    View v = getLayoutInflater().inflate(R.layout.newnote,null);
    adap = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, l);
    ListView list = (ListView) findViewById(R.id.tit);
    try {
        list.setAdapter(adap);
    }
    catch(Exception e){}
    layout.addView(v);

}

when clicking the add button a custom input dialog will appear to take the title of the note.单击添加按钮时,将出现一个自定义输入对话框以获取注释的标题。

 public void addnote(View view) {
    LayoutInflater layoutInflater = LayoutInflater.from(this);
    View promptView = layoutInflater.inflate(R.layout.addworddialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    // set prompts.xml to be the layout file of the alertdialog builder
    alertDialogBuilder.setView(promptView);
    final EditText input = (EditText) promptView.findViewById(R.id.nw);
    // setup a dialog window
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("ADD", (dialog, id) -> {
                // get user input and set it to result
                writer(input.getText().toString());

            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
    // create an alert dialog
    AlertDialog alertD = alertDialogBuilder.create();
    alertD.show();
    LinearLayout layout = (LinearLayout) findViewById(R.id.tn);
    addlist(titles,layout);
}
}

also, there are no errors during the complete process.此外,在整个过程中没有错误。
it's just not showing the new list layout它只是没有显示新的列表布局

alertD.show();
LinearLayout layout = (LinearLayout) findViewById(R.id.tn);
addlist(titles,layout);

Dialog.show does not block, so titles will probably not have changed when addlist is called, leading to the behavior you're seeing. Dialog.show不会阻塞,因此在addlisttitles可能不会改变,从而导致您看到的行为。

Furthermore changing the "titles.txt" file does not mean the list of titles read using reader() will change.此外,更改“titles.txt”文件并不意味着使用reader()读取的标题列表会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM