我正在努力与我的自定义列表视图。我想用我自定义的row.xml
文件填充我的列表视图,其中包含五个文本视图。我在raw
文件夹中有一个文本文件mytextfile.txt
。它看起来像这样:Android:如何使用列表中的字符串数组列表
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'类,而不是'私有列表 mAllConcerts;'你就必须'私有列表 mAllConcerts;' –
@DanielNugent嗨,我应该怎么得到的所有字符串值从文本文件转换为Concert实体类?感谢您的评论! – oxyt