Android:如何在List行中使用String数组列表

时间:2015-05-05 16:50:42

标签: java android arrays android-listview android-arrayadapter

我正在努力使用自定义列表视图。我想用我的自定义row.xml文件填充我的列表视图,该文件包含五个textview。我在mytextfile.txt文件夹中有一个文本文件raw。它看起来像这样:

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...

正如您所看到的,我从每一行创建了一个字符串数组(使用split方法,使用" - ")并将所有字符串数组放在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;
    }
}

4 个答案:

答案 0 :(得分:2)

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

JSON

所以你只是这样做:

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

}

答案 1 :(得分:1)

  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);}
    
  5. 仅使用String[] concertRow = concerts.getAllConcerts().get(position);,然后将文字设置为文字字段,因为在getView方法中只创建了一个ListView行。

答案 2 :(得分:1)

首先,您应该扩展BaseAdapter,而不是ArrayAdapter。 在方法getView中,您可以夸大row.xml布局。并找到你所有的TextViews。

Here是我的自定义适配器示例应用。

答案 3 :(得分:0)

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

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

你应该做得好。