【问题标题】:android - custom ListView, getView methodandroid - 自定义ListView、getView方法
【发布时间】:2013-01-26 17:11:32
【问题描述】:

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

这应该以 test 开始,以 test5 结束。我想getView() 方法应该被调用 6 次,因为 6 行被膨胀了?但是我在这个方法中添加了一个日志,发现它被调用了 20 多次!为什么会这样?

我的代码如下:

适配器类:

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

聊天活动:

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();

}

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

【问题讨论】:

    标签: android android-listview android-inflate


    【解决方案1】:

    我想 getView() 方法应该被调用 6 次,因为 6 行被夸大了?但是我在这个方法中添加了一个日志,发现它被调用了 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 块之外以支持滚动。

    【讨论】:

    • 非常感谢,这解决了问题。顺便说一句,我将 ListView 的尺寸更改为 match_parent,但 getView() 不断被多次调用。但是在我重新定位您提到的代码后,现在正确添加了消息。是否会导致任何性能问题?
    • 我不完全确定为什么getView() 被调用了这么多次,我看到您使用的是 EditText,当您启动 Activity 时键盘会立即打开吗? (键盘打开/关闭会导致布局重绘。) 至于性能问题:如果您没有注意到延迟,通常可以忽略它。一般规则是,如果您只想节省几毫秒,就不要花费数小时/数天来优化您的代码。
    • 键盘没有立即打开,因为我设置了 android:windowSoftInputMode="stateAlwaysHidden" 属性。不知道每次出现键盘都会重绘布局。。但是getView()方法调用还是太多,不知道是什么原因
    • 顺便问一下,我需要更改构造函数并为时间戳使用额外的 ArrayList 吗?在使用新时间再次调用 getView() 后,看不到任何其他可能阻止它被更改的可能性,旧的时间戳只会被所有消息覆盖
    • "我是否需要更改构造函数并为时间戳使用额外的 ArrayList?"您可以保留两个单独的列表,但这很容易出错。 (在未来的某个时候,您将不可避免地从一个列表中添加或删除数据,而不是另一个......)最好创建一个简单的 ChatText 类(或数据库中的表)来保存每个字符串、时间戳, 和任何其他数据合并到一个对象中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 2015-09-28
    相关资源
    最近更新 更多