Android:如何在列表行中使用字符串数组列表

Android: How to use List of String arrays inside List row

我正在为我的自定义列表视图而苦苦挣扎。我想用包含五个文本视图的自定义 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 class 来完成这项工作。我的其他文件看起来像...

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;
    }
}

菜单Concerts.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;
    }
}
  1. 你的ListView在哪里?你不在 onCreateView 方法中初始化它,你需要将 listAdapter 设置为 ListView.
  2. CustomAdapter getView 方法中,您不需要一直从 xml 膨胀视图。您必须重用创建的视图。检查 convertView:

    if(convertView ==null){ LayoutInflater inflater = LayoutInflater.from(getContext()); convertView = inflater.inflate(R.layout.concert_row, parent, false); }

  3. 对于适配器中的 TextView's,最好使用 ViewHilder 模式 - Example

  4. 你不需要这个周期:

    for(int i = 0; i < concerts.getAllConcerts().size(); i++) {
            String[] concertRow = concerts.getAllConcerts().get(i);}
    

仅使用 String[] concertRow = concerts.getAllConcerts().get(position);,然后将文本设置为文本字段,因为在 getView 方法中只创建了一个 ListView 行。

您必须在您的片段 XML 文件中创建一个列表视图(我假设您膨胀的视图就是那个)。然后为它分配一个 ID(我将使用 R.id.list 作为示例)。一旦你接到那个电话(在 onCreateView:

ListView lv = (ListView) view.findViewById(R.id.list);
// set the adapter
lv.setAdapter(listAdapter);

而且你应该很好。

我建议您使用对象的 ArrayList 而不是字符串数组来创建 CustomAdapter:

Public Class Concert {
private String dayName;
private String dayNumber;
// constructors, getter and setters
}

所以你只需要做:

   public class CustomAdapterextends BaseAdapter {

Context context;

protected List<Concert> listConcert;
LayoutInflater inflater;

public ListCarsAdapter(Context context, List<Concert> listConcert) {
    this.listConcert= listConcert;
    this.inflater = LayoutInflater.from(context);
    this.context = context;
}

public int getCount() {
    return listConcert.size();
}

public Concert getItem(int position) {
    return listConcert.get(position);
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {

        holder = new ViewHolder();
        convertView = this.inflater.inflate(R.layout.concert_row,parent, false);

        holder.concertDayName = (TextView) convertView
                .findViewById(R.id.concert_day_name);
        holder.concertDayNumber= (TextView) convertView
                .findViewById(R.id.concert_day_number);
        holder.concertMonthName = (TextView) convertView
                .findViewById(R.id.concert_month_name);
        holder.concertOnSaleNow = (TextView) convertView
                .findViewById(R.id.concert_on_sale_now);
        holder.concertPlaceName = (TextView) convertView
                .findViewById(R.id.concert_place_name);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Concert concert = listConcert.get(position);
    holder.concertDayName .setText(concert.getDayName());
    holder.concertDayNumber.setText(concert.getDayNumber());
    //etc.
    return convertView;
}

private class ViewHolder {
    TextView concertDayName;
    TextView concertDayNumber;
    Textview concertMonthName;
    Textview concertOnSaleNow;
    Textview concertPlaceName;
}

}

还有你的activity:

public class MenuConcerts extends Fragment {

ArrayList<Concert> arrayConcert = new ArrayList<Concert>();


    @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));
 // all the reading file logic here;
        ListAdapter listAdapter = new CustomerAdapter(getActivity(), arrayConcert);

        return view;
    }
}

首先你应该扩展 BaseAdapter,而不是 ArrayAdapter。 在方法 getView 中,您可以扩充 row.xml 布局。和 findById 你所有的 TextViews。

Here 是我的带有自定义适配器的示例应用程序。