Mariano Zorrilla
Mariano Zorrilla

Reputation: 7676

BaseAdapter gets me only the first position in ListView Android

I've a major isse with an adapter to show every single row item in a ListView and I only get the first value of the index position.

I've an array of comments from a webservice in android. It's an array inside an array.

The main JSONArray it's "noticias" with has a lot of the previews jsonobject. Now: "comments" it's another array.

This is my ContenidoNews.java class. I pass the "comment" array in a intent from the previews Activity:

public class ContenidoNews extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{

    private ActionBar bar;
    String tituloDetail;
    String fullDetail;
    String imagenDetail;
    String permalink;
    String author;
    String authorimg;
    String dayofmonth;
    String month;
    String views;
    String totalcomments;
    String arraycomments;
    private SquareImageView mainPic;
    private CircularImageView authorpic;
    private TextView authors;
    private TextView contentTxt;
    private ScrollView scollMain;
    private TextView mainTxt;
    private Animation animSlideDown;
    private RelativeLayout relativeAnim;
    private RelativeLayout namereceipt;
    private Typeface texto;
    private Button btnshare;

    // Universal Image Loader

    private DisplayImageOptions options;
    private ImageLoader il = ImageLoader.getInstance();
    private DisplayImageOptions opts;
    private File cacheDir;
    private String imgauthor;
    private TextView tvday;
    private TextView tvmonth;
    private Typeface textofat;
    private LinearLayout datestv;
    private WebView webview;
    private ArrayList links;
    private String urlscap;
    private String newstring;
    private String copyfull;
    private String youtube;
    private YouTubePlayerView youTubePlayerView;
    private String youtubeb;
    private String youtubec;
    private String youtubed;
    private TextView tvtotal;
    private TextView tvvisitas;
    private String youtubee;
    private String youtubef;
    private JSONArray arrayComment;
    ArrayList<HashMap<String, String>> arrayofcomments;
    private ListView listcomment;
    private ListAdapter adapter;
    private TextView numbcomment;
    private TextView tvcomment;

    static String AUTHOR = "comauthor";
    static String COMMENT = "comtext";

    // Grab URLs from text

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contenido);

        bar = getActionBar();
        bar.setBackgroundDrawable(getResources().getDrawable(R.drawable.gradiente));
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setTitle("");

        webview = (WebView) findViewById(R.id.wvhit);
        webview.getSettings().setJavaScriptEnabled(true);

        youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);

        Intent i = getIntent();
        // Get the result of titulo
        tituloDetail = i.getStringExtra("titulo").replace("&#8220;", "\"").replace("&#8221;", "\"").replace("&#8230;", "...").replace("&#8211;", "-");
        // Get the result of full
        fullDetail = i.getStringExtra("full");
        // Get the result of imagen
        imagenDetail = i.getStringExtra("imagen");
        // Get the result of permalink
        permalink = i.getStringExtra("permalink");
        // Get the result of author
        author = i.getStringExtra("author");
        // Get the result of authorimg
        authorimg = i.getStringExtra("authorimg");
        int urlimg = authorimg.lastIndexOf("s=128&amp;");
        imgauthor = "http://" + authorimg.substring(9, urlimg + 5);
        // Get the result of dayofmonth
        dayofmonth = i.getStringExtra("dayofmonth");
        // Get the result of month
        month = i.getStringExtra("month");
        // Get the result of views
        views = i.getStringExtra("views");
        // Get the result of total comments
        totalcomments = i.getStringExtra("totalcomments");
        // Get the result of array comments
        arraycomments = i.getStringExtra("arraycomments");

        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl(permalink);

        pullLinks(fullDetail);

        copyfull = fullDetail;

        new JSONParser().execute();

        texto = Typeface.createFromAsset(this.getAssets(),
                "Light.ttf");

        textofat = Typeface.createFromAsset(this.getAssets(),
                "Bold.ttf");

        listcomment = (ListView) findViewById(R.id.listofcomments);

        datestv = (LinearLayout) findViewById(R.id.datestv);

        tvday = (TextView) findViewById(R.id.tvday);
        tvmonth = (TextView) findViewById(R.id.tvmonth);

        tvday.setText(dayofmonth);
        tvmonth.setText(month);

        tvtotal = (TextView) findViewById(R.id.totalvisitas);
        tvtotal.setText(views);
        tvtotal.setTypeface(textofat);

        tvvisitas = (TextView) findViewById(R.id.TVvisitas);
        if(views.contentEquals("1")){
            tvvisitas.setText("VISITA");
        }else{
            tvvisitas.setText("VISITAS");
        }
        tvvisitas.setTypeface(texto);

        tvday.setTypeface(textofat);
        tvmonth.setTypeface(texto);

        mainPic = (SquareImageView) findViewById(R.id.fullimg);
        authorpic = (CircularImageView) findViewById(R.id.profile_settings_img);

        btnshare = (Button) findViewById(R.id.btnshare);

        ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);
        if (scrollView instanceof Parallaxor) {
            ((Parallaxor) scrollView).parallaxViewBy(mainPic, 0.6f);
        }

        relativeAnim = (RelativeLayout) findViewById(R.id.animtext);

        animSlideDown = AnimationUtils.loadAnimation(getApplicationContext(),
                R.animator.slidetodown);

        relativeAnim.startAnimation(animSlideDown);

        il.displayImage(imagenDetail, mainPic,opts,new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String s, View itemView) {

            }

            @Override
            public void onLoadingFailed(String s, View itemView, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View itemView, Bitmap bitmap) {
                SquareImageView imageView = (SquareImageView) itemView; 
                    if (bitmap != null) {
                        FadeInBitmapDisplayer.animate(imageView, 500);
                    }
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }

        });

        il.displayImage(imgauthor, authorpic,opts,new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String s, View itemView) {

            }

            @Override
            public void onLoadingFailed(String s, View itemView, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View itemView, Bitmap bitmap) {
                CircularImageView imageView = (CircularImageView) itemView; 
                    if (bitmap != null) {
                        FadeInBitmapDisplayer.animate(imageView, 500);
                    }
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }

        });

        contentTxt = (TextView) findViewById(R.id.textnoti);

        if(links.size() != 0 && urlscap.contains("youtu")){
            youTubePlayerView.setVisibility(View.VISIBLE);
            contentTxt.setText(newstring);
        }else{
            youTubePlayerView.setVisibility(View.GONE);
            contentTxt.setText(fullDetail);
        }

        authors = (TextView) findViewById(R.id.authors);
        authors.setText(author);
        authors.setTypeface(texto);

        mainTxt = (TextView) findViewById(R.id.tituloreceipt);
        mainTxt.setText(tituloDetail);
        mainTxt.setTypeface(texto);

        namereceipt = (RelativeLayout) findViewById(R.id.namereceipt);

        Resources res = getResources();
        final TypedArray myImages = res.obtainTypedArray(R.array.image);
        final Random random = new Random();

        //Genrate a random index in the range
        int randomInt = random.nextInt(myImages.length());

        // Generate the drawableID from the randomInt
        int drawableID = myImages.getResourceId(randomInt, -1);
        namereceipt.setBackgroundResource(drawableID);

        numbcomment = (TextView) findViewById(R.id.numbofcomments);
        numbcomment.setText(totalcomments);

        tvcomment = (TextView) findViewById(R.id.textforcomments);
        if(totalcomments.contentEquals("1")){
            tvcomment.setText("COMENTARIO");
        }else{
            tvcomment.setText("COMENTARIOS");
        }

    }

My AsyncTask

private class JSONParser extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            try {

                arrayofcomments = new ArrayList<HashMap<String, String>>();

                arrayComment = new JSONArray(arraycomments);

                for(int e = 0; e < arrayComment.length(); e++){
                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject come = arrayComment.getJSONObject(e);
                    map.put("comauthor", come.getString("comment_author"));
                    map.put("comtext", come.getString("comment_content"));

                    arrayofcomments.add(map);
                }

            } catch (Exception e1) {

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void args) {

            adapter = new ListAdapter(getApplicationContext(), arrayofcomments);

            listcomment.setAdapter(adapter);

        }
    }

The result of arrayComment it's:

[
    {
        "comment_author_url": "http://www.xxxx.com",
        "comment_type": "",
        "comment_author_IP": "xxxx",
        "comment_author": "Mariano xxxx",
        "comment_parent": "0",
        "comment_agent": "xxxx",
        "comment_karma": "0",
        "comment_author_email": "[email protected]",
        "comment_date": "2014-10-08 17:31:58",
        "comment_post_ID": "593",
        "comment_ID": "8",
        "comment_content": "Prueba",
        "user_id": "2",
        "comment_date_gmt": "2014-10-08 20:31:58",
        "comment_approved": "1"
    },
    {
        "comment_author_url": "",
        "comment_type": "",
        "comment_author_IP": "xxxx",
        "comment_author": "Susana A xxxx",
        "comment_parent": "0",
        "comment_agent": "xxxx",
        "comment_karma": "0",
        "comment_author_email": "[email protected]",
        "comment_date": "2014-10-08 08:28:00",
        "comment_post_ID": "593",
        "comment_ID": "7",
        "comment_content": "una clarísima definición",
        "user_id": "0",
        "comment_date_gmt": "2014-10-08 11:28:00",
        "comment_approved": "1"
    }
]

So far so good... now I need to pass all this to my adapter:

public class ListAdapter extends BaseAdapter {

        // Declare Variables
        Context context;
        LayoutInflater inflater;
        private ArrayList<HashMap<String, String>> data;
        private HashMap<String, String> resultp;

        public ListAdapter(Context context,
            ArrayList<HashMap<String, String>> d) {
            this.context = context;
            data = d;

        }

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

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            // Declare Variables
            TextView author;
            TextView comment;

            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View itemView = inflater.inflate(R.layout.comment_item, parent, false);

            resultp = new HashMap<String, String>();

            final int pos = position;

            resultp = data.get(position);

            Log.e("data", data.get(position).toString());

            author = (TextView) itemView.findViewById(R.id.tvauthorcomment);
            author.setText(resultp.get(ContenidoNews.AUTHOR));
            author.setTypeface(textofat);
            comment = (TextView) itemView.findViewById(R.id.tvofcomment);
            comment.setText(resultp.get(ContenidoNews.COMMENT));
            comment.setTypeface(texto);

            return itemView;
        }
    }

The result of Log.e("data full", data.toString()); its:

10-08 22:33:40.564: E/data full(26463): [{comtext=Prueba, comauthor=Mariano xxxx}, {comtext=una clarísima definición, comauthor=Susana A xxxx}]

Which, by the way, prints 7 times in the log... but it has the 2 values as I need.

Now... this it's what Log.e("data", data.get(position).toString()); gives me:

10-08 22:21:11.444: E/data(24618): {comtext=Prueba, comauthor=Mariano xxxx}

And:

author.setText(resultp.get(ContenidoNews.AUTHOR));
comment.setText(resultp.get(ContenidoNews.COMMENT));

In the adapter also only gave me the first value in my listview (comment_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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvauthorcomment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:paddingLeft="20dp"
            android:text="Autor Comentario"
            android:textColor="#707070"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tvofcomment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="5dp"
            android:paddingLeft="30dp"
            android:paddingRight="20dp"
            android:text="Contenido del comentario"
            android:textColor="#707070"
            android:textSize="19sp" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="#BBBBBB" />

    </LinearLayout>

</LinearLayout>

I can only get the first value... I can't fix this. I'm stock since a lot of hours and nothing. I don't understand what it's the problem. Please, help!

I would thanks any answer in advance!

EDIT: This is my contenido.xml with the scrollview and the listview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.renderas.suup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" xmlns:app1="http://schemas.android.com/apk/res/com.mkiisoft.masradio">

    <WebView
        android:id="@+id/wvhit"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <com.mkiisoft.masradio.utils.SquareImageView
        android:id="@+id/fullimg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:id="@+id/datestv"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="190dp"
        android:background="#90000000" >

        <TextView
            android:id="@+id/tvday"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:text="04"
            android:textColor="#FFF"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/tvmonth"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="Octubre"
            android:textColor="#FFF"
            android:textSize="24sp" />

    </LinearLayout>

    <uk.co.chrisjenx.paralloid.views.ParallaxScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="none" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="240dp" >

            <FrameLayout
                android:id="@+id/imagecontainer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RelativeLayout
                    android:id="@+id/namereceipt"
                    android:layout_width="fill_parent"
                    android:layout_height="80dp"
                    android:layout_gravity="bottom"
                    android:background="@drawable/bggreenyl" >

                    <TextView
                        android:id="@+id/tituloreceipt"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:gravity="top|left"
                        android:paddingBottom="8dp"
                        android:paddingLeft="12dp"
                        android:paddingRight="12dp"
                        android:paddingTop="8dp"
                        android:text="Noticia Titulo"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textColor="#FFF"
                        android:textSize="24sp" />
                </RelativeLayout>
            </FrameLayout>

            <RelativeLayout
                android:id="@+id/animtext"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@id/imagecontainer"
                android:background="#FFFFFF" >

                <com.mkiisoft.masradio.utils.CircularImageView
                    android:id="@+id/profile_settings_img"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:src="@drawable/emptypro" />

                <TextView
                    android:id="@+id/authors"
                    android:layout_width="wrap_content"
                    android:layout_height="80dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_toRightOf="@id/profile_settings_img"
                    android:gravity="center_vertical"
                    android:text="Medium Text"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#606060"
                    android:textSize="21sp" />

                <LinearLayout
                    android:id="@+id/linearviews"
                    android:layout_width="wrap_content"
                    android:layout_height="22dp"
                    android:layout_below="@id/authors"
                    android:layout_marginLeft="15dp"
                    android:layout_marginTop="8dp"
                    android:orientation="horizontal" >

                    <ImageView
                        android:id="@+id/imgvisitas"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center"
                        android:layout_marginTop="2dp"
                        android:alpha="0.7"
                        android:src="@drawable/eyeviewsblack" />

                    <TextView
                        android:id="@+id/totalvisitas"
                        android:layout_width="wrap_content"
                        android:layout_height="20dp"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:text="7"
                        android:textColor="#606060"
                        android:textSize="17sp" />

                    <TextView
                        android:id="@+id/TVvisitas"
                        android:layout_width="wrap_content"
                        android:layout_height="20dp"
                        android:gravity="center_vertical"
                        android:paddingLeft="5dp"
                        android:text="VISITAS"
                        android:textColor="#606060"
                        android:textSize="17sp" />

                </LinearLayout>

                <Button
                    android:id="@+id/btnshare"
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
                    android:layout_below="@id/linearviews"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginTop="12dp"
                    android:background="@drawable/share_btn"
                    android:text="COMPARTIR"
                    android:textColor="@color/checktxt" />

                <TextView
                    android:id="@+id/textnoti"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/btnshare"
                    android:paddingBottom="20dp"
                    android:paddingLeft="12dp"
                    android:paddingRight="12dp"
                    android:paddingTop="10dp"
                    android:text="Texto de la noticia"
                    android:textColor="#707070"
                    android:textSize="18sp" />

                <com.google.android.youtube.player.YouTubePlayerView
                    android:id="@+id/youtubeplayerview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/textnoti"
                    android:layout_marginBottom="20dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginTop="5dp" />

                <LinearLayout
                    android:id="@+id/linearofcomments"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:layout_below="@id/youtubeplayerview"
                    android:layout_marginLeft="15dp"
                    android:gravity="center_vertical|left" >

                    <TextView
                        android:id="@+id/numbofcomments"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:text="0"
                        android:textColor="#707070"
                        android:textSize="23sp" />

                    <TextView
                        android:id="@+id/textforcomments"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:layout_marginLeft="5dp"
                        android:text="COMENTARIOS"
                        android:textColor="#707070"
                        android:textSize="23sp" />

                </LinearLayout>

                <ListView
                    android:id="@+id/listofcomments"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/linearofcomments"
                    android:layout_marginBottom="30dp"
                    android:layout_marginTop="5dp" >

                </ListView>

            </RelativeLayout>
        </RelativeLayout>
    </uk.co.chrisjenx.paralloid.views.ParallaxScrollView>

</RelativeLayout>

Upvotes: 1

Views: 1303

Answers (1)

Harsha Vardhan
Harsha Vardhan

Reputation: 3344

We shouldn’t use ListView inside the ScrollView because the list view is already ScrollView, the items in list view are already scrollable..

If you use then the list view will show only one item from the adapter.

Check this post ListView inside scrollview for more details...

Upvotes: 3

Related Questions