如何在Android中的ListView项之前放置数字

如何在Android中的ListView项之前放置数字

在我的列表中,数据显示如下:

test 1
test 2
test 3


但是我想给他们看

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


我怎样才能做到这一点。请帮助。这是我到目前为止所做的代码。

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

}

最佳答案

您应该制作自己的“定制适配器类”并实现其方法。 。 。

这是“定制适配器”类的整个示例:

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

        }


之后可以像这样使用Adapter:

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

关于java - 如何在Android中的ListView项之前放置数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15973073/

10-10 15:16