0

I am just starting with android, so I don't have much to show of what I have so far...

I am trying to make a to-do list. I have seen tutorials on how to do this with a menu for input and have the tasks displayed in a ListView, however I would like the user to input the task into an EditText instead. I was using this (which doesn't use a ListView) to just have a button click make a new TextView underneath the EditText, however I couldn't add buttons next to the TextView which would remove it (when the user finishes a task).

Using a ListView seems much easier for this, but how can I have the EditText input go to a ListView with all the tasks and buttons to remove them (permanently, not just set visibility to 0)? I would also like the tasks to be stored, so the user can close the app and return with the tasks still there.

Any ideas? I haven't made much progress.

Community
  • 1
  • 1
LunarLlama
  • 229
  • 2
  • 11
  • From a quick scan of the tutorial. This covers inputting the data via an EditText and then and I assume then making it available to the ListView. I think the Storing and retrieving section will cover this latter aspect. – MikeT Aug 27 '16 at 04:00

1 Answers1

0

I recommend you to use RecyclerView instead of Listview. compile this in your Gradle

    compile 'com.levelupstudio:expandable-recyclerview:1.0.1'

Here is the mainActivity.java

public class MainActivity extends ActionBarActivity implements    
RecyclerViewAdapter.OnItemClickListener{

private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter myRecyclerViewAdapter;

EditText nameField;
Button btnAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);

    linearLayoutManager =
            new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    /*
    linearLayoutManager =
            new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    */

    myRecyclerViewAdapter = new RecyclerViewAdapter(this);
    myRecyclerViewAdapter.setOnItemClickListener(this);
    myRecyclerView.setAdapter(myRecyclerViewAdapter);
    myRecyclerView.setLayoutManager(linearLayoutManager);

    nameField = (EditText)findViewById(R.id.namefield);
    btnAdd = (Button)findViewById(R.id.addbutton);
    btnAdd.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            String newName = nameField.getText().toString();

          Context context= getApplicationContext();
                myRecyclerViewAdapter.add(0,newName);
            Toast.makeText(context,"You added" +newName.toUpperCase()+ "in your view",Toast.LENGTH_LONG).show();

        }
    });
}

 @Override
public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
    Toast.makeText(this,
            "Remove " + position + " : " + item.getItemName(),
          Toast.LENGTH_SHORT).show();
}}

And AdapterClass for Recycler

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {

private List<String> itemsName;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;

public RecyclerViewAdapter(Context context){
    layoutInflater = LayoutInflater.from(context);
    itemsName = new ArrayList<String>();
}

@Override
public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = layoutInflater.inflate(R.layout.list_item, parent, false);
    return new ItemHolder(itemView, this);
}

@Override
public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
    holder.setItemName(itemsName.get(position));

}

@Override
public int getItemCount() {
    return itemsName.size();
}

public void setOnItemClickListener(OnItemClickListener listener){
    onItemClickListener = listener;
}

public OnItemClickListener getOnItemClickListener(){
    return onItemClickListener;
}

public interface OnItemClickListener{
    public void onItemClick(ItemHolder item, int position);
}

public void add(int location, String iName){
    itemsName.add(location, iName);
    notifyItemInserted(location);
}


public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    private RecyclerViewAdapter parent;
    TextView textItemName;

    public ItemHolder(View itemView, RecyclerViewAdapter parent) {
        super(itemView);
        itemView.setOnClickListener(this);
        this.parent = parent;
        textItemName = (TextView) itemView.findViewById(R.id.item_name);
    }

    public void setItemName(CharSequence name){
        textItemName.setText(name);
    }

    public CharSequence getItemName(){
        return textItemName.getText();
    }

    @Override
    public void onClick(View v) {
        final OnItemClickListener listener = parent.getOnItemClickListener();
        if(listener != null){
            listener.onItemClick(this, getPosition());
        }
    }
}}

and your xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <EditText
        android:id="@+id/namefield"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/addbutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add"/>
</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/myrecyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

list_xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_marginTop="16dp"
    android:background="@android:color/holo_blue_light"
    android:orientation="vertical">

    <TextView
        android:id="@+id/item_name"
        android:layout_centerVertical="true"
        android:layout_margin="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</RelativeLayout>
Niroj
  • 1,114
  • 6
  • 29