0

I'm working on a project in Eclipse, and after googling some resources, I can't find a basic tutorial to achieve what I want to do.

I need to make a list view with simply Strings from an arraylist in it. Nothing fancy - and I'd like to able to slide the items off the screen or press a button to remove them from the list.

Is there a simple way to do this? Please share tutorials if you know any that could help me with this particular problem.

3
  • Do you want to show a single string in a listview? Commented Mar 31, 2014 at 4:09
  • An arraylist of strings, so a list of strings. E.g. {"Cow", "Plant", "Tree"} Commented Mar 31, 2014 at 4:14
  • I posted my answer what you are expecting you just use it and tell me.. Commented Mar 31, 2014 at 4:29

4 Answers 4

1

I made this list view for you and you can use this one for a example and use this And if you have any doubts on this listview comment me..

public class MainActivity extends Activity {
    private ArrayList<String> mListItems;
    private ListView listView;
    private ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = (ListView)findViewById(R.id.listView1);
        mListItems = new ArrayList<String>();
        for(String name : mStrings)
        {
            mListItems.add(name);
        }

        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, mListItems);
        listView.setAdapter(adapter);   


        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position,
                    long i) {
                String name = (String) adapter.getItemAtPosition(position);
                MainActivity.this.adapter.remove(name);         
            }
        });
    }


    private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler","Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler" };
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This did exactly what I wanted in a few lines. Perfect.
1

There is nice "Swipe to dismiss" project on GitHub: Android-SwipeToDismiss, that you can use together with the example ListViews included on this thread. Good luck :-)

Comments

0

Assuming you have a ListView with an id listview in your activity_main.xml the following code should work.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<String> values = new ArrayList<String>();
        int rows = 10;

        for(int i = 0; i < rows; i ++) {
            values.add("row " + i);
        }

        ListView listView = (ListView) findViewById(R.id.listview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
        listView.setAdapter(adapter);
    }
}

You will have to look into Adapters to get a full understanding of how ListView works. Here is a very good video: https://www.youtube.com/watch?v=N6YdwzAvwOA.

Comments

0
public class YourActivity extends Activity {

private ListView lv;

public void onCreate(Bundle saveInstanceState) {
     super.onCreate(saveInstanceState);
     setContentView(R.layout.your_layout);

     lv = (ListView) findViewById(R.id.your_list_view_id);

     // Instanciating an array list (you don't need to do this, 
     // you already have yours).
     List<String> your_array_list = new ArrayList<String>();
     your_array_list.add("foo");
     your_array_list.add("bar");

     // This is the array adapter, it takes the context of the activity as a 
     // first parameter, the type of list view as a second parameter and your 
     // array as a third parameter.
     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_list_item_1,
             your_array_list );

     lv.setAdapter(arrayAdapter); 
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.