Android ListView包含多个项目

时间:2016-08-07 23:31:43

标签: android listview

如何使用下面的照片获取ListView。 我用了这段代码:

enter code here public class ListClass extends Activity
{
    ListView listView;
    String[] values = { "A", "B", "C", "D" };
    List<String> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState)
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            listView = (ListView) findViewById(R.id.listView);
            arrayList = new ArrayList<String>();

            Collections.addAll(arrayList, values);

            final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, arrayList);
            listView.setAdapter(arrayAdapter);

        }
}

结果:pic 1

pic 1

如何像pic 2一样添加项目?

pic 2 - with new Items

4 个答案:

答案 0 :(得分:2)

以下是自定义列表视图的一个很好的示例:) http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter

创建   - 列表项的模型   - 列出项目布局   - 自定义适配器

答案 1 :(得分:2)

答案 2 :(得分:1)

我已经使用过此代码了。它奏效了。

public class Amordad extends ListActivity
{

    private String[] SHIFT;
    private String[] DAY_NIGHT;
    private String[] NUM;

    @Override
    protected void onCreate(Bundle savedInstanceState)
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.general_layout);

            refresh();
        }

    private void refresh()
        {

            int s = 12;
            SHIFT = new String[s];
            DAY_NIGHT = new String[s];
            NUM = new String[s];

            // 1 to s=12

            for (int i = 0; i < s; i = i + 1)
                {
                    NUM[i] = (i + 1) + "";

                }

            for (int i = 0; i < s; i += 4)
                {
                    SHIFT[i] = "A";
                    SHIFT[i + 1] = "A";

                }
            for (int i = 3; i < s; i += 4)
                {
                    SHIFT[i] = "B";
                    SHIFT[i - 1] = "B";

                }

            for (int i = 0; i < s; i += 2)
                {
                    DAY_NIGHT[i] = "Day";

                }

            for (int i = 1; i < s; i += 2)
                {
                    DAY_NIGHT[i] = "Night";

                }

            setListAdapter(new AA());

        }

    class AA extends ArrayAdapter<String>
        {

            public AA()
            {
                super(Amordad.this, R.layout.row_general_i, DAY_NIGHT);
            }

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

                    LayoutInflater in = getLayoutInflater();
                    View row = in.inflate(R.layout.row_general_i, parent, false);

                    TextView numbers = (TextView) row.findViewById(R.id.numbers);
                    TextView shifts = (TextView) row.findViewById(R.id.shifts);
                    TextView day_night = (TextView) row.findViewById(R.id.day_night);

                    numbers.setText(NUM[position]);
                    shifts.setText(SHIFT[position]);
                    day_night.setText(DAY_NIGHT[position]);

                    // I set color for "A" Blue (hex code) and "B" Red
                    // I used toString().contains here and equals ... witch
                    // one is true or better!? I don`t know.
                    if (SHIFT[position].toString().contains("A"))
                        {
                            shifts.setTextColor(Color.parseColor("#0000FF"));

                        } else if (SHIFT[position].equals("B"))
                        {
                            shifts.setTextColor(Color.RED);
                        } else
                        {

                        }
                    // I set color for "Day" Gray (hex code)
                    if (DAY_NIGHT[position].equals("Day"))
                        {
                            day_night.setTextColor(Color.parseColor("#555555"));

                        } else if (SHIFT[position].equals("Night"))
                        {
                            day_night.setTextColor(Color.RED);
                        }
                    return (row);
                }

        }

}

结果!Result Code 1

答案 3 :(得分:0)

ArrayAdapter不会这样做,你需要一个自定义的BaseAdapter。

搜索http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/

等教程

它将涉及:

  1. class MyAdapter extends BaseAdapter ViewHolder
  2. 和单元格的自定义XML,其中您的图片和文字位于水平LinearLayout