Ahmad Mirzaei
Ahmad Mirzaei

Reputation: 31

Android ListView with several Items

How do I get a ListView with the photo below. I used this code:

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

        }
}

Result:pic 1

pic 1

How add items Like pic 2?

pic 2 - with new Items

Upvotes: 2

Views: 182

Answers (4)

Ahmad Mirzaei
Ahmad Mirzaei

Reputation: 31

I`ve used this code. It worked.

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 !Result Code1

Upvotes: 1

Dhiraj Sharma
Dhiraj Sharma

Reputation: 4859

Here is a good example on Custom List View :) http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter

Create - Model of List Item - List Item Layout - Custom Adapter

Upvotes: 2

TWL
TWL

Reputation: 6646

ArrayAdapter will not do, you need a custom BaseAdapter.

Search for tutorials such as http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/

It will involve:

  1. a class MyAdapter extends BaseAdapter with a ViewHolder
  2. and a custom XML for the cell with your image and text in a horizontal LinearLayout

Upvotes: 0

Related Questions