BaseAdapter只让我在ListView Android中获得第一个位置

时间:2014-10-09 01:37:19

标签: java android listview adapter baseadapter

我有一个主要的东西,有一个适配器来显示ListView中的每一个单项,我只得到索引位置的第一个值。

我从android中的webservice获得了一系列评论。它是一个数组内的数组。

主要的JSONArray它的“noticias”有很多预览jsonobject。现在:“评论”它是另一个阵列。

这是我的ContenidoNews.java类。我从预览Activity:

中传递了一个意图中的“comment”数组
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");
        }

    }

我的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);

        }
    }

arrayComment的结果是:

[
    {
        "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": "xxxx@xxxx.com",
        "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": "xxxxxx@yahoo.com.ar",
        "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"
    }
]

到目前为止很好......现在我需要将所有这些传递给我的适配器:

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;
        }
    }

Log.e的结果(“data full”,data.toString());其:

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}]

顺便说一句,在日志中打印7次......但是我有2个值。

现在......这就是Log.e(“data”,data.get(position).toString());给了我:

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

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

在适配器中也只给了我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>

我只能得到第一个值......我无法解决这个问题。我从很多小时起都没有库存。我不明白这是什么问题。请帮助!

我会提前感谢任何答案!

编辑:这是我的contenido.xml,包含scrollview和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>

1 个答案:

答案 0 :(得分:3)

我们不应该在ScrollView中使用ListView,因为列表视图已经是ScrollView,列表视图中的项目已经可以滚动了。

如果您使用,则列表视图将仅显示适配器中的一个项目。

查看此帖子ListView inside scrollview了解更多详情......