2014-10-09 30 views
1

我有一個適配器來顯示ListView中的每一行的項目,我只獲得索引位置的第一個值。BaseAdapter只讓我在ListView中的第一個位置Android

我有一個來自android中的web服務的評論數組。它是一個數組內的數組。

主JSONArray它是「消息報」與有很多預覽jsonobject。現在:「評論」這是另一個數組。

這是我的ContenidoNews.java類。我通過 「評論」 陣列從預覽活動目的:

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": "[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" 
    } 
] 

到目前爲止好.. 。現在我需要將所有這些傳遞給我的適配器:

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());的結果

其:

​​

順便說一句,在日誌中打印7次......但它有我需要的2個值。 (「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)); 

在適配器也只給了我第一個值在我的列表視圖(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與滾動視圖和列表視圖

<?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> 
+0

的列表視圖中添加XML ....在此之前,U具有列表視圖內滾動視圖 – 2014-10-09 01:47:42

回答

3

,因爲列表視圖已經滾動型,在列表視圖中的項目已經滾動,我們不應該使用ListView中滾動型內..

如果使用,那麼列表視圖將只顯示適配器中的一個項目。

檢查這個帖子ListView inside scrollview瞭解更多詳情...

+0

感謝響應!我認爲你是對的......我忘記了這一點。我會檢查鏈接。 順便說一句...我可以顯示一排佈局意圖的列表視圖? – 2014-10-09 02:34:59

相關問題