将所选字段从ArrayList加载到ListView。

时间:2013-05-20 04:16:23

标签: java android listview arraylist cursor

我想只将我的ArrayList中的选定字段加载到ListView。 我找不到那个例子,所以我问。

我有一个结构的ArrayList如下:

ArrayList<LogInfo> logInfoArray

LogInfo类的字段如下:

public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
public int[] times; //contains fields of calendar class

我想在“时间”和“代码”

中的每一行选定字段中放入我的ListView

我怎样才能实现这一目标?如果可能,我想使用光标。

1 个答案:

答案 0 :(得分:0)

您可以使用从ArrayAdapter延伸到ArrayList<LogInfo>的自定义适配器。

然后,您可以取消适配器的getView(..)方法,以便在Listview的行中设置所需的字段。

<强>更新

来自Android Custom Adapters

的示例
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<LogInfo> {

  private final List<LogInfo> list;
  private final Activity context;

  public InteractiveArrayAdapter(Activity context, List<LogInfo> list) {
    super(context, R.layout.rowbuttonlayout, list);
    this.context = context;
    this.list = list;
  }

  static class ViewHolder {
    protected TextView text1, text2;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
      LayoutInflater inflator = context.getLayoutInflater();
      view = inflator.inflate(R.layout.rowbuttonlayout, null);
      final ViewHolder viewHolder = new ViewHolder();
      viewHolder.text1 = (TextView) view.findViewById(R.id.label1);
      viewHolder.text2 = (TextView) view.findViewById(R.id.label2);

      view.setTag(viewHolder);

    } else {
      view = convertView;

    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text1.setText(list.get(position).getName1());
    holder.text2.setText(list.get(position).getName2());
    return view;
  }
}