-2

I have a file named "music.txt" in directory res/raw. It contains a list of songs and authors separated by ";", like this:

Bohemian rhapsody;Queen

Piano man;Billy Joel

Born to run;Bruce Springsteen

I want to use the file to populate a multicolumn ListView. Currently it works with the first column, so it shows only the name of the song, and I don't know how to implement the second column. I tried to replace the "activity_list_item" layout from the ArrayAdapater for a custom layout so I could place two elements on the same row inside TextViews, but it doesn't seem to work (it can't find the layout I created).

Here is the code:

public class Main2Activity extends AppCompatActivity {

ListView list;

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

    list = findViewById(R.id.musicList);

    loadData();

}

public void loadData(){
    List<String> musicArray = new ArrayList<String>();
    String line;

    InputStream is = this.getResources().openRawResource(R.raw.music);
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));

    try {
        if (is != null) {
            while ((line = reader.readLine()) != null) {
                musicArray.add(line.split(";")[0]);
            }
        }
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String data[] = musicArray.toArray(new String[musicArray.size()]);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, android.R.id.text1, data);
    list.setAdapter(adapter);
}

}

My custom LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".Main3Activity"
    android:weightSum="100">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_weight="50"
        android:text="TextView"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_weight="50"
        android:text="TextView"/>

</LinearLayout>

Thanks!

  • "it can't find the layout I created" – Try cleaning and rebuilding the project (under the Build menu), and if that doesn't work, Invalidate Caches/Restart (under File). There's a glitch in recent Android versions with newly created layouts. – Mike M. May 08 '20 at 06:42
  • Thanks, but doesn't work. Just in case I'm doing something wrong: I created the layout in res/layout as a layout resource file. Seems like the obvious place but maybe it's not, is that the place where this type of layout should be? –  May 08 '20 at 06:54
  • Yep, that's it. It'd go in the same place as the other layouts you're using; e.g., `R.layout.activity_main2`. – Mike M. May 08 '20 at 06:56
  • First of all, your list contains only the names of singers. Create an object and fill your list accordingly. Then follow this [answer](https://stackoverflow.com/a/18529511/8956604). Or my idea is that you should use custom adapter. – Kasım Özdemir May 08 '20 at 06:59
  • What do you mean by "it can't find the layout I created"? What exactly is happening? – Ryan M May 08 '20 at 11:08
  • The question is solved don't worry. In case anyone has the same issue: I was using "android.R.layout.your_custom_layout_id" instead of "R.layout.your_custom_layout_id". –  May 08 '20 at 11:24

3 Answers3

0

Possibly you can use RecyclerView with GridLayoutManager

GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 4); // change number of columns as you want
layoutManager.setOrientation(RecyclerView.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
Moutain
  • 21
  • 5
  • It doesn't work, it appears red and tells me to create parameters and variables. –  May 08 '20 at 06:56
0

You're right that you will need to define your own list item layout. This will be accessed in your class via R. not android.R..

You will also need to define your own Adapter, by extending the ArrayAdapter class. In this new adapter you will define how to split your Strings and present them in 2 distinct TextView views within your custom layout.

codebod
  • 686
  • 6
  • 17
0

Create music object:

class Music {
    String name;
    String song;

    public Music(String name, String song) {
        this.name = name;
        this.song = song;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSong() {
        return song;
    }

    public void setSong(String song) {
        this.song = song;
    }
}

Change your list:

ArrayList<Music> musicList = new ArrayList<>();

Fill your list:

while ((line = reader.readLine()) != null) {
      String [] array = line.split(";");
      musicList.add(new Music(array[0],array[1]));
}

And your adapter:

ArrayAdapter adapter = new ArrayAdapter(this, R.layout.your_custom_layout_id, R.id.textView1, musicList) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView text1 = (TextView) view.findViewById(R.id.textView1);
            TextView text2 = (TextView) view.findViewById(R.id.textView2);

            text1.setText(musicList.get(position).getName());
            text2.setText(musicList.get(position).getSong());
            return view;
        }
    };

list.setAdapter(adapter);
Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35