Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Gooday
Below is my static code in java(android studio) and i have a listview linked to a layout and a class which must dynamically populate recent posts in a forum app i am designing. each listitem has drawable image of user who posted and information about the question posted on the same list item. i want the list to be created dynamically lets say a user posts "need help with etc " and the string[] userpost updates. Profile image is also loaded on the list item dynamically on the app. in other words i want to replace the static string "need help with etc " with string from runtime. The profile image as well with a image link at runtime.
********************************small sample code from
activity_main class
**************************
String [] web= { "need help with etc "};

Integer[] imageId = {
R.drawable.profile_image,
}
********************************
code for the custom list array
**********************************************************
code for the custom list array


package com.example.vtutor;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomList extends ArrayAdapter<String> {
public static int managani;
    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;
    public CustomList(Activity context,
                      String[] web, Integer[] imageId) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.web = web;
        this.imageId = imageId;

    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_single, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        TextView zuva = (TextView) rowView.findViewById(R.id.tipedate);
        txtTitle.setText(web[position]);
        imageView.setImageResource(imageId[position]);
       return rowView;
    }
}



***********************************************************************************
code from the activity_main class

What I have tried:

String [] userpost = { "need help with etc "};

    Integer[] imageId = {
            R.drawable.profile_image,
                       }
Posted
Updated 6-Aug-21 3:14am
v3
Comments
Richard MacCutchan 5-Aug-21 9:08am    
1. Where will the users post these messages?
2. How will your app get notified of new messages?
David Crow 5-Aug-21 16:32pm    
Do you have a CustomList object that is associated with the ListView? Do you also have an ArrayList<String> object that is associated with this adapter object? If you have both, then simply add String items to the list and call the adapter's notifyDataSetChanged() method.
PCOMM Engineering 6-Aug-21 9:13am    
Thank you so much for the response. yes i do have an object connected to the Listview. Well may you help me with code to add items to such a string dynamically, i.e String web[] = {"what must i put hear to load user information at runtime" }.... or an alternative to this may be helpful. My idea was to have a custom listview layout which load user data like profile pic on e left of an item and specific information on the right of the image as string. The image and the string are user profile in my forum app. Each user will be displayed in its on listviewitem going down...
David Crow 8-Aug-21 10:46am    
"My idea was to have a custom listview layout which load user data like profile pic on e left of an item and specific information on the right of the image as string. The image and the string are user profile in my forum app. Each user will be displayed in its on listviewitem going down..."

You would need a "row" layout that gets inflated in the adapter's onCreateViewHolder() method. You can design such a layout any way you wish, but a simple version might look like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView android:id="@+id/image"
        android:scaleType="centerCrop"
        android:layout_gravity="center"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView android:id="@+id/tvText"
        android:textSize="20sp"
        android:textColor="@android:color/holo_blue_dark"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

1 solution

Thank you so much for the response. yes i do have an object connected to the Listview. Well may you help me with code to add items to such a string dynamically, i.e String web[] = {"what must i put hear to load user information at runtime" }.... or an alternative to this may be helpful. My idea was to have a custom listview layout which load user data like profile pic on e left of an item and specific information on the right of the image as string. The image and the string are user profile in my forum app. Each user will be displayed in its on listviewitem going down...
 
Share this answer
 
Comments
Richard Deeming 6-Aug-21 9:41am    
If you want to reply to a comment, click the "Reply" button next to the comment and post a comment.

Do not post your comment as a "solution" to your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900