0

I am newbie with this language. I want to put the items in my gridview into the list view but i do not know how.

This is my code in MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gv= (GridView) findViewById(R.id.gv);
    Button b = (Button) findViewById(R.id.lv);

    String[] items = new String[]{
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H",
            "I",
            "J",
            "K",
            "L",
            "M",
            "N",
            "O",
            "P",
            "Q",
            "R",
            "S",
            "T",
            "U",
            "V",
            "W",
            "X",
            "Y",
            "Z"
    };
    b.setOnClickListener( new View.OnClickListener(){
     @Override
     public void onClick(View v){
      Intent intent = new Intent("com.example.user.gridviewtolistview.Listview");
         startActivity(intent);
       }
       }
    );
    final ArrayAdapter<String> gridViewArrayAdapter = new ArrayAdapter<String>
            (this,android.R.layout.simple_list_item_1, items);

    // Data bind GridView with ArrayAdapter (String Array elements)
    gv.setAdapter(gridViewArrayAdapter);
}

This is the MainActivity UI

MainActivityUI

When I clicked on the button "Convert into Listview", it will go into another activity.

This is the Listview UI which looks like this:

Listview UI

I want to show the items in the gridview in the listview dynamically.

1 Answers1

0

First add listview in xml

<ListView
      android:id="@+id/listview"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
</ListView>

Then in Java file.

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

If you want to create layout which you show in photo. You can achieve it by creating custom adapter or via using simple_list_item_2

Custom Adapter/Listview means you can specify your own layout.

To learn how to use simple_list_item_2 use following link.

How to set both lines of a ListView using simple_list_item_2?

To learn how to create custom adapter or listview

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

ListView and GridView are old. You can use RecyclerView. It has much more functionality then list/grid view.

Learn RecyclerView : http://www.androidhive.info/2016/01/android-working-with-recycler-view/

Community
  • 1
  • 1
Naitik Kundalia
  • 199
  • 1
  • 1
  • 19