仅将一个项目设置为listview的适配器

时间:2012-09-29 13:35:24

标签: android listview arraylist android-arrayadapter

我想从我的自定义ArrayList中只向适配器发送一个项目。活动开始时,我只发送一个项目。然后我想对我的Activity实现gestureListener,如果我检测到向上或向下滑动Action,我会向适配器发送另一个项目。我该如何实现呢?

我有自定义的ArrayAdapter,我传递了List。但我只想发送一个项目:

public class InteractionsAdapter extends  ArrayAdapter<Message>{

  Context context; 
int layoutResourceId;    
List<Message> messages = null;
private Activity activity;
public InteractionsAdapter(Context context,  int layoutResourceId,List<Message> messages) {
    super(context, layoutResourceId, messages);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.messages = messages;
}

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

    if(row == null)
    {

        (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.interactions_item, parent, false);

        holder = new InteractionHolder();
        holder.interactionId= (TextView)row.findViewById(R.id.interactionId);
        holder.interactionAbove = (TextView)row.findViewById(R.id.interactionAbove);
        holder.interactionBelow = (TextView)row.findViewById(R.id.interactionBelow);
        holder.interactionMessage = (TextView)row.findViewById(R.id.interactionMessage);
        holder.interactionDate = (TextView)row.findViewById(R.id.interactionDate);

        row.setTag(holder);
    }
    else
    {
        holder = (InteractionHolder)row.getTag();
    }

    Message message = messages.get(position);
    Log.i("in doinbackground",Integer.toString(messages.size()));

        Log.i("position",Integer.toString(position));

        holder.interactionId.setText(Integer.toString(message.getMessageId()));
        holder.interactionAbove.setText(Integer.toString(position));
        holder.interactionBelow.setText(Integer.toString(messages.size()-position-1));
        holder.interactionMessage.setText(message.getMessageBody());
        holder.interactionDate.setText(message.getMessageDate().toString());

    return row;
}

static class InteractionHolder
{
    TextView interactionId;
    TextView interactionDate;
    TextView interactionAbove;
    TextView interactionBelow;
    TextView interactionMessage;

}

}

我尝试使用ArrayAdapter,只传递一个项目,但它不起作用。

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,只需添加一个

@Override
public int getCount() {
    return 1;
}

它运作正常。