I think one way would be to override getView in ArrayAdaptor. I've tried to make an example that adds items to a List View.
Here's the XML for the activity.
[HIGH] <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android

addingBottom="@dimen/activity_vertical_margin"
android

addingLeft="@dimen/activity_horizontal_margin"
android

addingRight="@dimen/activity_horizontal_margin"
android

addingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/listView1"
/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:text="Add"
android

nClick="addToList"/>
</RelativeLayout> [/HIGH]This Layout, dated_message.xml is for the items in the list view.
[HIGH]<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/rowDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="12sp" />
<TextView
android:id="@+id/rowMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/rowDate"
android:textSize="18sp" />
</RelativeLayout>
[/HIGH]A class for my messages:
[HIGH]package com.example.test2;
import java.text.DateFormat;
import java.util.Date;
public class DatedMessage {
private String message;
private String date;
DatedMessage(String message){
this.message = message;
Date now = new Date();
DateFormat df = DateFormat.getDateTimeInstance();
date = df.format(now);
}
public String getMessage(){
return message;
}
public String getDate(){
return date;
}
}[/HIGH]And adapter class for the above:
[HIGH]package com.example.test2;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MessagesAdapter extends ArrayAdapter<DatedMessage>{
int resource;
public MessagesAdapter(Context context, int _resource, List<DatedMessage> items){
super(context, _resource, items);
resource = _resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout view;
if (convertView == null) {
view = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(inflater);
li.inflate(resource, view, true);
}
else {
view = (LinearLayout) convertView;
}
DatedMessage dm = this.getItem(position);
TextView dateView = (TextView) view.findViewById(R.id.rowDate);
dateView.setText(dm.getDate());
TextView message = (TextView) view.findViewById(R.id.rowMessage);
message.setText(dm.getMessage());
return view;
}
}
[/HIGH]The main activity.
[HIGH]package com.example.test2;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText editText;
MessagesAdapter ma;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.listView1);
editText = (EditText)findViewById(R.id.editText1);
ArrayList<DatedMessage> messages = new ArrayList<DatedMessage>();
ma = new MessagesAdapter
(this, R.layout.dated_messages, messages);
listView.setAdapter(ma);
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
DatedMessage dm = ma.getItem(position);
Toast.makeText(getApplicationContext(), dm.getDate() + "\n" + dm.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
public void addToList(View v){
ma.add(new DatedMessage(editText.getText().toString()));
ma.notifyDataSetChanged();
editText.setText("");
}
}
[/HIGH]