* activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

* MainActivity.java

public class MainActivity extends AppCompatActivity {

    private DbHelper dbHelper = new DbHelper(this);
    private LibraryHelper libraryHelper = new LibraryHelper();

    private TextView subject;

    private DrawerLayout drawerLayout;
    private MainFragment mainFragment = new MainFragment(this);

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

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        
        ArrayList<Memory> memoryList = dbHelper.memoryList();
        
        MemoryAdapter memoryAdapter = new MemoryAdapter(memoryList);
        recyclerView.setAdapter(memoryAdapter);
	}
    
}

* recycler_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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/url"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
    
</LinearLayout>

* MemoryAdapter.java

public class MemoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static class MemoryViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        TextView url;

        MemoryViewHolder(View view) {
            super(view);

            title = view.findViewById(R.id.title);
            url = view.findViewById(R.id.url);
        }

    }

    private ArrayList<Memory> memoryList;

    public MemoryAdapter(ArrayList<Memory> memoryList) {
        this.memoryList = memoryList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);

        return new MemoryViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MemoryViewHolder memoryViewHolder = (MemoryViewHolder) holder;

        memoryViewHolder.title.setText(memoryList.get(position).getTitle());
        memoryViewHolder.url.setText(memoryList.get(position).getUrl());
    }

    @Override
    public int getItemCount() {
        return memoryList.size();
    }

}

'android' 카테고리의 다른 글

입력된 url을 WebView로 열기  (0) 2020.02.03
Spinner 사용법  (0) 2020.02.03
레이아웃 사용자정의 사용  (0) 2020.02.03
버튼 사용자정의 사용  (0) 2020.02.03
NicePatch(png 이미지 확대시)  (0) 2020.02.03
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,