android - 自定义ListView,getView方法

时间:2013-01-26 17:11:33

标签: android android-listview android-inflate

好吧,我投票删除了我之前的类似问题以保持清洁。问题:我正在尝试使用自定义ListView在聊天应用中显示消息。我现在设法根据用户的输入设置文本,但我在getView()方法中遇到了一些麻烦。 您可以在下面的图片中看到问题:膨胀的布局添加在错误的位置。

enter image description here

这应该以test开始,以test5结束。我想假设6行被充气,getView()方法应被调用6次?但我添加了一个Log to this方法,并发现它被称为20次以上!为什么这样?

enter image description here

我的代码如下:

适配器类:

public final class ChatAdapter extends BaseAdapter {
private Context context;
ViewHolder holder;

private ArrayList<String> messages;

public ChatAdapter(Context context, ArrayList<String> chat) {

    this.context = context;
    this.messages = chat;
    Log.d("ChatAdapter", "called constructor");

}

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

public Object getItem(int position) {
    return messages.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup viewGroup) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row, null);

        holder = new ViewHolder();
        holder.message = (TextView) convertView
                .findViewById(R.id.tvMessage);
        holder.timestamp = (TextView) convertView.findViewById(R.id.tvTS);
        holder.indicator = (ImageView) convertView
                .findViewById(R.id.imgInd);

        String message = (String) getItem(position);
        holder.message.setText(message);
        holder.timestamp.setText(generateTimestamp());

        convertView.setTag(holder);

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

    }
    Log.d("ChatAdapter", "called getView");
    return convertView;
}

@SuppressLint("SimpleDateFormat")
public String generateTimestamp() {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    String ts = sdf.format(new Date());

    return ts;
}

public static class ViewHolder {
    public TextView message;
    public TextView timestamp;
    public ImageView indicator;
}

ChatActivity:

public class ChatActivity extends ListActivity {

private EditText input;
private String tmpMessage;
ListView lv;
ChatAdapter adapter;
ArrayList<String> messages;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lis);
    Button send = (Button) findViewById(R.id.btnSend);
    input = (EditText) findViewById(R.id.etInput);
    lv = getListView();

    messages = new ArrayList<String>();
    Log.d("Chat", "arrayList created");
    adapter = new ChatAdapter(this, messages);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO
        }
    });

    lv.setAdapter(adapter);

    send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            sendMessage();
        }
    });
}

public void sendMessage() {

    tmpMessage = input.getText().toString();
    input.setText("");


    messages.add(tmpMessage);

    adapter.notifyDataSetChanged();

}

我缺少什么(除了一些大脑)?任何帮助是极大的赞赏。

1 个答案:

答案 0 :(得分:1)

  

我认为getView()方法应该被调用6次,因为6行被夸大了?但我添加了一个Log to this方法,并发现它被称为20次以上!为什么这样?

强制getView()被多次调用的常见原因是使用wrap_content作为ListView的高度。

getView()中的此代码:

String message = (String) getItem(position);
holder.message.setText(message);
holder.timestamp.setText(generateTimestamp()); // You will need to make a permanent timestamp someday

应该在if-else块之外支持滚动。