Home > Software design >  How to make only the list title visible in the list menu and not the entire content of the list item
How to make only the list title visible in the list menu and not the entire content of the list item

Time:07-14

I'm trying to make a simple note-taking app. I tried to display the notes in the listview. But when I add more content to the note it displays the entire content in the main menu of the list view. How to make only the title of the list visible in the Listview and the content visible only when we open the list item?

MainActitvity.java

package com.example.notes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    static   ArrayList<String> notes=new ArrayList<>();
    static ArrayAdapter arrayAdapter;
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.add_note_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
         super.onOptionsItemSelected(item);
         if(item.getItemId()==R.id.add_note){
             Intent intent=new Intent(getApplicationContext(),NoteEditorActivity.class);
             startActivity(intent);
             return true;
         }
         return false;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listview=(ListView) findViewById(R.id.listView);
        SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);
        HashSet<String> set=(HashSet<String>)sharedPreferences.getStringSet("notes",null);
        if(set==null) {
            notes.add("Example note");
        }
        else{
            notes=new ArrayList(set);
        }
        notes.add("Note 1");
     arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,notes) ;
        listview.setAdapter(arrayAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
        Intent intent=new Intent(getApplicationContext(),NoteEditorActivity.class);
        intent.putExtra("noteId",i);
        startActivity(intent);
    }
});
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
 final int itemToDelete=i;
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("are you sure?")
                .setMessage("do you want to delete?")
                .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        notes.remove(itemToDelete);
                        arrayAdapter.notifyDataSetChanged();
                        SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);
                        HashSet<String> set=new HashSet<>(MainActivity.notes);
                        sharedPreferences.edit().putStringSet("notes",set).apply();
                    }
                })
                .setNegativeButton("no",null)
                .show();
        return true;
    }
});
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/note1"
    tools:context=".MainActivity">
    <ListView
        android:id="@ id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

note_editor_activity.java

package com.example.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import com.google.android.material.textfield.TextInputLayout;
import java.util.HashSet;
public class NoteEditorActivity extends AppCompatActivity {
int noteId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_editor);
        EditText editText=(EditText) findViewById(R.id.editText);
        Intent intent=getIntent();
        noteId=intent.getIntExtra("noteId",-1);
if(noteId!=-1){
    editText.setText(MainActivity.notes.get(noteId));
}
else{
    MainActivity.notes.add("");
    noteId=MainActivity.notes.size()-1;
    MainActivity.arrayAdapter.notifyDataSetChanged();
}
editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
MainActivity.notes.set(noteId,String.valueOf(charSequence));
MainActivity.arrayAdapter.notifyDataSetChanged();
        SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);
        HashSet<String> set=new HashSet<>(MainActivity.notes);
        sharedPreferences.edit().putStringSet("notes",set).apply();
    }
    @Override
    public void afterTextChanged(Editable editable) {
    }
});
    }
}

acitivity_node-editor.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/olo"
tools:context=".NoteEditorActivity">
<EditText
    android:id="@ id/editText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10"
    android:gravity="start|top"
    android:inputType="textMultiLine"
    android:padding="0dp"
    tools:layout_editor_absoluteX="118dp"
    tools:layout_editor_absoluteY="91dp" />
    </androidx.constraintlayout.widget.ConstraintLayout>

add_note_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@ id/add_note" android:title="Add note"></item>
</menu>

CodePudding user response:

In my opinion I think you should use two edittext, one for title and one for the content then save both in a hasmap. Using the title as key then using it for the list view content.

CodePudding user response:

The note_create.java

EditText title=findViewById(R.id.title);
EditText body=findViewById(R.id.body);
HashMap<String,String>all=new HashMap<String,String>();
all.put(title.getText().toString(),body.getText().toString());
ArrayList<String>head=new ArrayList<String>();
head.addAll(all.keySet());

CodePudding user response:

The note_create.java

EditText title=findViewById(R.id.title);
EditText body=findViewById(R.id.body);
HashMap<String,String>all=new HashMap<String,String>();
all.put(title.getText().toString(),body.getText().toString());
ArrayList<String>head=new ArrayList<String>();
head.addAll(all.keySet());

Then add the head ArrayList to your listview

For viewing

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String read=(String)getItemAtPosition(arg2);
if(head.contains(read);
{
String reads=all.get(read);
}
});

CodePudding user response:

You can pass the read String to another intent to view This is how I did mine

  • Related