【发布时间】:2015-05-05 16:50:41
【问题描述】:
我正在为我的自定义列表视图而苦苦挣扎。我想用包含五个文本视图的自定义row.xml 文件填充我的列表视图。我在raw 文件夹中有一个文本文件mytextfile.txt。它看起来像这样:
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...
如您所见,我从每一行创建一个字符串数组(使用带有“-”的拆分方法)并将所有字符串数组放入ArrayList 中。我不知道如何创建我的 CustomAdapter 类来完成这项工作。我的其他文件看起来像...
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(我什至不知道我是否在正确的道路上)
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(这是将显示列表的片段)
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;
}
}
【问题讨论】:
-
我认为更好的方法是创建一个
Concert类,而不是private List<String[]> mAllConcerts;你应该有private List<Concert> mAllConcerts; -
@DanielNugent 嗨,我应该如何将文本文件中的所有字符串值获取到该 Concert 实体类中?感谢您的评论!
标签: java android arrays android-listview android-arrayadapter