2016-04-21 34 views
0

Listview大小爲10個項目。直到它不顯示Listview中的任何內容。我在上面的適配器中添加了Item of List,但無法獲得單行的Listview。任何人都請幫我解決我在適配器中的錯誤?Listview display nothing

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.util.List; 
import bughunters.tashfik.chooseapp.Model.Category; 
import bughunters.tashfik.chooseapp.Model.Comment; 
import bughunters.tashfik.chooseapp.R; 
public class CommentViewAdapter extends BaseAdapter { 

private Context context; 

private List<Comment> messagesItems; 


// public static List<String> imagelist; 

public CommentViewAdapter(Context context, List<Comment> navDrawerItems) { 
    Toast.makeText(context,"Sixe ids"+String.valueOf(navDrawerItems.size()),Toast.LENGTH_LONG).show(); 
    this.context = context; 
    this.messagesItems = navDrawerItems; 

} 

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

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

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 


    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

    convertView = mInflater.inflate(R.layout.comments_list_item, null); 

    TextView textViewProfileName = (TextView) convertView.findViewById(R.id.textViewProfileName); 
    TextView textViewMesssage = (TextView) convertView.findViewById(R.id.textViewMesssage); 
    TextView textViewTime = (TextView) convertView.findViewById(R.id.textViewTime); 
    Toast.makeText(context,messagesItems.get(position).getUsername(),Toast.LENGTH_LONG).show(); 
    ImageView imageViewUserProfilePic = (ImageView) convertView.findViewById(R.id.imageViewUserProfilePic); 


    textViewProfileName.setText(messagesItems.get(position).getUsername()); 
    textViewMesssage.setText(messagesItems.get(position).getMessage()); 
    textViewTime.setText(messagesItems.get(position).getTime()); 


    return convertView; 
} 

}

+1

你能也張貼comments_list_item.xml文件? –

+0

你可以發佈MainActivity文件嗎? –

+0

請發佈你的活動代碼 –

回答

1

替換下面的代碼: -

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.util.List; 
import bughunters.tashfik.chooseapp.Model.Category; 
import bughunters.tashfik.chooseapp.Model.Comment; 
import bughunters.tashfik.chooseapp.R; 
public class CommentViewAdapter extends BaseAdapter { 

private Context context; 

private List<Comment> messagesItems; 


// public static List<String> imagelist; 

public CommentViewAdapter(Context context, List<Comment> navDrawerItems) { 
    Toast.makeText(context,"Sixe ids"+String.valueOf(navDrawerItems.size()),Toast.LENGTH_LONG).show(); 
    this.context = context; 
    this.messagesItems = navDrawerItems; 

} 

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

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

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

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

    View rowView=mInflater.inflate(R.layout.comments_list_item, null,true); 

    TextView textViewProfileName = (TextView) rowView.findViewById(R.id.textViewProfileName); 
    TextView textViewMesssage = (TextView) rowView.findViewById(R.id.textViewMesssage); 
    TextView textViewTime = (TextView) rowView.findViewById(R.id.textViewTime); 

    ImageView imageViewUserProfilePic = (ImageView) convertView.findViewById(R.id.imageViewUserProfilePic); 

    textViewProfileName.setText(messagesItems.get(position).getUsername()); 
    textViewMesssage.setText(messagesItems.get(position).getMessage()); 
    textViewTime.setText(messagesItems.get(position).getTime()); 

    return rowView; 
} 
} 

與下面的代碼檢查MainActivity代碼: -

CommentViewAdapter customAdapter = new CommentViewAdapter(getApplicationContext(), levelLists(your ArrayList Put here)); 
    listview.setAdapter(customAdapter); 
+0

現在它正在工作... – Krupa

+1

謝謝你Krupa .... –