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!