Ravindu Ravindu - 3 months ago 11
Android Question

How to put a number before ListView item in Android

In my list the data is shown like this,

test 1
test 2
test 3


But I want to show them like,

1) test 1
2) test 2
3) test 3


How can I do that. Plese help. This is the code I have done so far.

public class AppointmentList extends Activity{

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.select_to_delete);

String[] myStringArray = new String[] { "test 1", "test 2", "test 3"};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myStringArray);

ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
}

}

Answer

You should make your own Custom Adapter Class and implement its methods . . .

Here is the whole example of Custom Adapter class :

 private class Adapter extends BaseAdapter {
            private ArrayList<String> array;
            private Context ctx;

            public Adapter(Context ctx) {
                this.ctx = ctx;
                array = new ArrayList<String>();
            }

            @Override
            public int getCount() {
                return this.array.size();
            }

            @Override
            public Object getItem(int position) {
                return this.array.get(position);
            }

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

            /**
             * add a String Item in a List
             * 
             * @param item
             */
            public void addItem(String item) {
                this.array.add(item);
                this.notifyDataSetChanged();
            }

            /**
             * Delete an Item from a List
             * 
             * @param position
             */
            public void deleteItem(int position) {
                this.array.remove(position);
                this.notifyDataSetChanged();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View newView;

                if (convertView == null) {
                    newView = View.inflate(ctx, R.layout.item_layout, null);
                } else {
                    newView = convertView;
                }

                String txt = (String) this.getItem(position);

//   Here is what you're looking for:
                ((TextView) newView.findViewById(R.id.item_numb)).setText(""
                        + (position + 1) + ". ");
//   End;
                ((TextView) newView.findViewById(R.id.item_text)).setText(txt);

                return newView;
            }

        }

You can use Adapter afterwards like this :

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do_list);

        ListView ls = (ListView) findViewById(R.id.listView);
        Adapter adapter = new Adapter(this);
        ls.setAdapter(adapter);
    }