public class HuruSocialActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List postList;
private PostAdapter postAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_huru_social);
// Initialize RecyclerView and its layout manager
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// Initialize the list of posts
postList = new ArrayList();
// Add some dummy posts for testing
postList.add(new Post(“John Doe”, “Just had a great day at the beach! #sun #sand #fun”, R.drawable.beach));
postList.add(new Post(“Jane Smith”, “Excited for the weekend! Any plans?”, R.drawable.weekend));
// Initialize the adapter and attach it to the RecyclerView
postAdapter = new PostAdapter(postList);
recyclerView.setAdapter(postAdapter);
}
// Class representing a post
private class Post {
private String username;
private String content;
private int imageResource;
public Post(String username, String content, int imageResource) {
this.username = username;
this.content = content;
this.imageResource = imageResource;
}
public String getUsername() {
return username;
}
public String getContent() {
return content;
}
public int getImageResource() {
return imageResource;
}
}
// RecyclerView adapter for displaying posts
private class PostAdapter extends RecyclerView.Adapter {
private List postList;
public PostAdapter(List postList) {
this.postList = postList;
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post, parent, false);
return new PostViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
Post post = postList.get(position);
holder.usernameTextView.setText(post.getUsername());
holder.contentTextView.setText(post.getContent());
holder.imageView.setImageResource(post.getImageResource());
}
@Override
public int getItemCount() {
return postList.size();
}
// ViewHolder class for holding post views
private class PostViewHolder extends RecyclerView.ViewHolder {
private TextView usernameTextView;
private TextView contentTextView;
private ImageView imageView;
public PostViewHolder(@NonNull View itemView) {
super(itemView);
usernameTextView = itemView.findViewById(R.id.username_text_view);
contentTextView = itemView.findViewById(R.id.content_text_view);
imageView = itemView.findViewById(R.id.image_view);
}
}
}
}
