Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:23:28+00:00 2026-06-17T21:23:28+00:00

alright, I voted to delete my previous similar question to keep it clean. The

  • 0

alright, I voted to delete my previous similar question to keep it clean. The matter: I’m trying to use a custom ListView for displaying messages in a chat app. I now managed to set the text according to user’s input but I have some troubles in the getView() method.
You can see the problem at the pictures below: the inflated layouts are added at the wrong position.

enter image description here

This should start with test and end with test5. I suppose the getView() method should have been called 6 times since 6 rows were inflated? But I added a Log to this method and figured out that it was called like 20+ times! Why so?

enter image description here

My code goes below:

the adapter class:

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

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

}

What am I missing (except of some brain)? Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:23:29+00:00Added an answer on June 17, 2026 at 9:23 pm

    I suppose the getView() method should have been called 6 times since 6 rows were inflated? But I added a Log to this method and figured out that it was called like 20+ times! Why so?

    A common cause that forces getView() to be called multiple times is using wrap_content for the ListView’s height.

    Also this code in getView():

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

    Should be outside the if-else block to support scrolling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I'm trying to use the basic document.getElementsByTagName function, but each time I do,
Alright, so this is linked to a previous question I had. I am very
Alright guys, i'm trying to use the variable in the getIndividualMap function but i'm
Alright I created some custom classes for my project sourced from this tutorial ,
Alright, here I am again trying to write code from scratch and I can't
Alright, I've got what should be an easy question but there are literally no
Alright, right now I am trying to create a basic four in a row
Alright, I know I've asked similar questions, but I feel this is hopefully a
Alright, so I've done a bit of searching and trying with no luck. I'm
Alright Gents, Im trying to search a vb.net combo box for an item. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.