undercoverindian
12/27/2017 - 12:46 AM

Scollable list

Create a scrollable list to display elements

public class DayAdapter extends BaseAdapter {

    public Context context;
    public ArrayList<Day> dayArray;

    public DayAdapter(Context context, ArrayList<Day> dayArray){
        this.context = context;
        this.dayArray = dayArray;
    }

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

    @Override
    public Object getItem(int i) {
        return dayArray.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder;
        //int i is basically position

        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.custom_list, null);
            holder = new ViewHolder();

            holder.summary = (TextView) view.findViewById(R.id.textViewSummary);
            holder.maxTemp = (TextView) view.findViewById(R.id.textViewTemp);

            view.setTag(holder);
        }

        else
            holder = (ViewHolder) view.getTag();

        Day day = dayArray.get(i);
        holder.summary.setText(String.valueOf(day.getSummary()));
        holder.maxTemp.setText(String.valueOf(day.getMaxTemp()));

        return view;
    }

    public static class ViewHolder{
        public TextView summary;
        public TextView maxTemp;
    }
}
- Add ListView to main XML layout file
- Create Object class, ListView will be populated with an array of these objects

1. Add custom_list.xml to res -> layouts, change RelativeLayout height, padding

<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="50dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    tools:background="@color/colorPrimary">
    
    //if doing android:background instead, material design effects will not show

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Summary"
        android:paddingLeft="60dp"
        android:id="@+id/textViewSummary"
        android:textSize="20dp"
        android:textIsSelectable="false"
        android:textColor="@color/white"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Temp"
        android:paddingLeft="230dp"
        android:id="@+id/textViewTemp"
        android:textSize="20dp"
        android:textIsSelectable="false"
        android:textColor="@color/white"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"/>
    
</RelativeLayout>

2. Create ObjectAdapter class, extend BaseAdapter, and implement methods

3. Initialize ListView in MainActivity

ListView listView;

Day first = new Day("good summary", 30);
Day second = new Day("bad summary", 40);
ArrayList<Day> dayArray = new ArrayList<Day>(Arrays.asList(first, second));

DayAdapter adapter = new DayAdapter(getApplicationContext(), dayArray);
listView.setAdapter(adapter);

4. Create ListView onItemClickListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  Day currentDay;

  @Override
  public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

      currentDay = (Day) adapter.getItem(i);
      String summary = currentDay.getSummary();

  }
});