뷰홀더를 사용한 리스트뷰를 간단히 만들었습니다. 해당소스는 버튼을 누르면 리스트 뷰에 데이터가 들어가는 동작을 합니다.

MainActivity.java

package com.example.shinseongil.testlist;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private ListView mListView;
    private Button mButton;
    private ArrayList mTestArr; // 데이터 ArrayList
    private MainAdapter mMainAdapter; // 어댑터 클래스
    private boolean mIsInitAdapter = false; //어댑터 초기화 여부
    private int mNumber; //리스트에 표시하는 숫자

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton = (Button) findViewById(R.id.test_btn);
        mListView = (ListView) findViewById(R.id.test_list);
        mTestArr = new ArrayList<>();
        mMainAdapter = new MainAdapter(this);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                initList();
            }
        });

    }

    private void initList(){
        if (!mIsInitAdapter) {
            mListView.setAdapter(mMainAdapter);
        } else {
            mMainAdapter.notifyDataSetChanged();
        }
    }


    /**
     * 데이터 초기화
     */
    private void initData() {
        for (int i = 0; i < 10; i++) {
            TestData testData = new TestData();
            testData.id = mNumber;
            testData.name = "테스트" + mNumber;
            testData.date = "" + new Date().getTime();
            mTestArr.add(testData);
            mNumber++;
        }
    }

    private class MainAdapter extends BaseAdapter {
        private Context mContext;
        LayoutInflater mInflater;

        public MainAdapter(Context context) {
            this.mContext = context;
            mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

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

        @Override
        public Object getItem(int position) {
            return mTestArr.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TestViewHolder testViewHolder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.test_list_item, null);
                testViewHolder = new TestViewHolder();
                testViewHolder.testId = (TextView) convertView.findViewById(R.id.test_list_id);
                testViewHolder.testName = (TextView) convertView.findViewById(R.id.test_list_name);
                testViewHolder.testDate = (TextView) convertView.findViewById(R.id.test_list_date);
                convertView.setTag(testViewHolder);
            } else {
                testViewHolder = (TestViewHolder) convertView.getTag();

            }
            testViewHolder.testId.setText("" + mTestArr.get(position).id);
            testViewHolder.testName.setText(mTestArr.get(position).name);
            testViewHolder.testDate.setText(mTestArr.get(position).date);

            return convertView;
        }
    }

    /**
     * 홀더 클래스
     */
    public class TestViewHolder {
        public TextView testId;
        public TextView testName;
        public TextView testDate;
    }

    /**
     * Data 클래스
     */
    public class TestData {
        int id;
        String name;
        String date;
    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.shinseongil.testlist.MainActivity">

<Button
android:id="@+id/test_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Touch Button"
/>

<ListView
android:id="@+id/test_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/test_btn" />
</RelativeLayout>


test_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/test_list_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="New Text" />

<TextView
android:id="@+id/test_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text" />

<TextView
android:id="@+id/test_list_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text" />
</LinearLayout>





728x90
반응형

+ Recent posts