-1

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ankit Rath
  • 59
  • 7

1 Answers1

0
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.

Furthermore changing the "titles.txt" file does not mean the list of titles read using reader() will change.

Kiskae
  • 24,655
  • 2
  • 77
  • 74