I am struggling with my custom listview. I want to populate my list view with my custom row.xml file which contains five textviews. I have a text file mytextfile.txt in the raw folder. It look like this:
SUN-9-JULY-On Sale Now-New York, Time Square
SAT-15-JULY-On Sale Now-London, National Gallery
MON-23-JULY-On Sale Now-Paris, The Eiffel Tower
// More lines here...
As you can see I create a string array from each line (using split method with "-") and put all string arrays inside a ArrayList. I don't know how to create my CustomAdapter class to make this work. My other files looks like...
Concerts.java
public class Concerts {
private List<String[]> mAllConcerts;
public List<String[]> getAllConcerts() {
return mAllConcerts;
}
public Concerts() {
mAllConcerts = new ArrayList<>();
InputStream inputStream = null;
InputStreamReader inputReader = null;
BufferedReader bufferedReader;
String line;
try {
inputStream = MainActivity.getGlobalContext().getResources()
.openRawResource(R.raw.mytextfile.txt);
inputReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputReader);
while (( line = bufferedReader.readLine()) != null) {
String [] mConcertDetails = line.split("-");
mAllConcerts.add(mConcertDetails);
}
}
catch (IOException e) {
// cathing...
}
finally {
// closing...
}
}
}
CustomAdapter.java (I don't even know if I am on the right path here)
public class CustomAdapter extends ArrayAdapter<String> {
TextView concertDayName;
TextView concertDayNumber;
TextView concertMonthName;
TextView concertOnSaleNow;
TextView concertPlaceName;
Concerts concerts;
public CustomAdapter(Context context, String[] values) {
super(context, R.layout.concert_row, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.concert_row, parent, false);
concerts = new Concerts();
concertDayName = (TextView) view.findViewById(R.id.concert_day_name);
concertDayNumber = (TextView) view.findViewById(R.id.concert_day_number);
concertMonthName = (TextView) view.findViewById(R.id.concert_month_name);
concertOnSaleNow = (TextView) view.findViewById(R.id.concert_on_sale_now);
concertPlaceName = (TextView) view.findViewById(R.id.concert_place_name);
for(int i = 0; i < concerts.getAllConcerts().size(); i++) {
String[] concertRow = concerts.getAllConcerts().get(i);
concertDayName.setText(concertRow[0]);
concertDayNumber.setText(concertRow[1]);
concertMonthName.setText(concertRow[2]);
concertOnSaleNow.setText(concertRow[3]);
concertPlaceName.setText(concertRow[4]);
}
return view;
}
}
MenuConcerts.java (This is the fragment that list will be shown)
public class MenuConcerts extends Fragment {
String[] concertDetails;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.concerts_layout, container, false);
((MainActivity) getActivity()).setActionBarTitle(getString(R.string.title_spelningar));
ListAdapter listAdapter = new CustomerAdapter(getActivity(), concertDetails);
return view;
}
}
Concertclass, and instead ofprivate List<String[]> mAllConcerts;you would haveprivate List<Concert> mAllConcerts;