Skip to content Skip to sidebar Skip to footer

How Lists (specifically, Recyclerview With Cardviews) In Android Work

Forgive me for asking this question, but I am new to Android development and although I am trying to go through the basics on the developer.android.com website, most of the samples

Solution 1:

I have put together an example. I will update it with explanation when I get home.

activity_main.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:padding="16dp"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Title"android:id="@+id/etTitle" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Description"android:id="@+id/etDescription" /><Buttonandroid:id="@+id/btnAddItem"android:text="Add Item"android:layout_width="match_parent"android:layout_height="wrap_content" /><ListViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:divider="@android:color/transparent"android:layout_weight="1"android:id="@+id/lvList" /></LinearLayout>

list_row_layout.xml

<android.support.v7.widget.CardViewxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_margin="8dp"android:layout_height="match_parent"><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/txtTitle"android:padding="12dp"android:layout_width="match_parent"android:textColor="@android:color/black"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/txtDescription"android:padding="12dp"android:layout_width="match_parent"android:textColor="@android:color/black"android:layout_height="wrap_content" /></LinearLayout></android.support.v7.widget.CardView>

ListData.java

publicclassListData {
    String title;
    StringDescription;

    publicStringgetTitle() {
        return title;
    }

    publicvoidsetTitle(String title) {
        this.title = title;
    }

    publicStringgetDescription() {
        returnDescription;
    }

    publicvoidsetDescription(String description) {
        Description = description;
    }
}

ListAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

publicclassListAdapterextendsBaseAdapter {
    ArrayList<ListData> myList = newArrayList<ListData>();
    Context context;

    publicListAdapter(Context context, ArrayList<ListData> myList) {
        this.myList = myList;
        this.context = context;
    }

    @OverridepublicintgetCount() {
        return myList.size();
    }

    @Overridepublic ListData getItem(int position) {
        return myList.get(position);
    }

    @OverridepubliclonggetItemId(int position) {
        return0;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        ListViewHolderview= (ListViewHolder) convertView;
        if (view == null) {
            view = newListViewHolder(context);
        }
        ListDatalog= getItem(position);
        view.setLog(log);
        return view;
    }
}

ListViewHolder

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;


publicclassListViewHolderextendsLinearLayout {
    Context mContext;
    ListData mLog;

    publicListViewHolder(Context context) {
        super(context);
        mContext = context;
        setup();
    }

    publicListViewHolder(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setup();
    }

    privatevoidsetup() {
        LayoutInflaterinflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.list_row_layout, this);
    }

    publicvoidsetLog(ListData log) {
        mLog = log;
        TextViewtvTitle= (TextView) findViewById(R.id.txtTitle);
        tvTitle.setText(mLog.getTitle() + "");
        TextViewtvDescription= (TextView) findViewById(R.id.txtDescription);
        tvDescription.setText(mLog.getDescription() + "");
    }
}

MainActivity

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;


publicclassMainActivityextendsActionBarActivity {
    Button btnAddItem;
    ListView lvList;
    ArrayList<ListData> myList = newArrayList<ListData>();
    ListAdapter listAdapter;
    EditText etTitle, etDescription;
    Stringtitle="", description = "";

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvList = (ListView) findViewById(R.id.lvList);
        listAdapter = newListAdapter(MainActivity.this, myList);
        lvList.setAdapter(listAdapter);
        etTitle = (EditText) findViewById(R.id.etTitle);
        etDescription = (EditText) findViewById(R.id.etDescription);
        btnAddItem = (Button) findViewById(R.id.btnAddItem);
        btnAddItem.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                title = etTitle.getText().toString();
                description = etDescription.getText().toString();
                if (title.length() == 0) {
                    title = "Not Title";
                }
                if (description.length() == 0) {
                    description = "No Description";
                }
                ListDatamLog=newListData();
                mLog.setTitle(title);
                mLog.setDescription(description);
                myList.add(mLog);
                listAdapter.notifyDataSetChanged();
            }
        });
        lvList.setOnItemClickListener(newAdapterView.OnItemClickListener() {
            @OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListDatamLog= listAdapter.getItem(position);
                Toast.makeText(MainActivity.this, "Title: " + mLog.getTitle() + "  Description: " + mLog.getDescription(), Toast.LENGTH_LONG).show();
            }
        });
    }
}

Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "your application id"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile'com.android.support:appcompat-v7:21.0.3'compile'com.android.support:cardview-v7:21.+'
}

What it should look likeenter image description here

Post a Comment for "How Lists (specifically, Recyclerview With Cardviews) In Android Work"