Reputation: 863
Screenshot Of My Layout
I added TranscriptMode in both xml and java. But its not working... Xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.buckydroid.chat.ContactList">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/contact_lists"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"/>
</RelativeLayout>
The listview is a part of fragment I call it when the index 0 of bottombar layout gets Clicked.. Fragment Class Java Code :
public class ContactList extends Fragment implements AdapterView.OnItemClickListener{
private ListView contactList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.contact_list,container,false) ;
contactList = (ListView)view.findViewById(R.id.contact_lists);
contactList.setAdapter(new ContactListAdapter(getActivity()));
return view;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
class RowData {
String user_name;
int user_img;
String user_msg;
int message_status;
public RowData(String user_name, int user_img, String user_msg, int message_status) {
this.user_name = user_name;
this.user_img = user_img;
this.user_msg = user_msg;
this.message_status = message_status;
}
}
class viewHolder{
CircleImageView user_img;
ImageView message_stats;
TextView user_name;
TextView user_message;
viewHolder(View v){
user_img = (CircleImageView)v.findViewById(R.id.user_img_list);
user_name = (TextView)v.findViewById(R.id.contact_name);
user_message = (TextView)v.findViewById(R.id.user_message);
message_stats = (ImageView)v.findViewById(R.id.message_status);
}
}
class ContactListAdapter extends BaseAdapter{
ArrayList<RowData> list;
Context context;
ContactListAdapter(Context c){
context = c;
list = new ArrayList<RowData>();
String[] username = {"Bucky","Marie","Melvin","Rheo","Emi","Lighter","Toco","Ravi","krushna"};
String[] message = {"Bunch Of Shitty Texts . ","Bunch Of Shitty Texts . ","Bunch Of Shitty Texts . ","Bunch Of Shitty Texts . ","Bunch Of Shitty Texts .","Bunch Of Shitty Texts . ","Bunch Of Shitty Texts . u","Bunch Of Shitty Texts . Fuck You","Bunch Of Shitty Texts"};
int[] user_image = {R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man,R.drawable.man};
int[] message_stats = {R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage,R.drawable.unreadmessage};
for (int i = 0; i <9; i++){
list.add(new RowData(username[i],user_image[i],message[i],message_stats[i]));
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
viewHolder holder = null;
if (row==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.contact_list_style,parent,false);
holder = new viewHolder(row);
}else{
Toast.makeText(context, "GRRRRRRRRRRR", Toast.LENGTH_SHORT).show();
}
RowData temp = list.get(position);
holder.user_name.setText(temp.user_name);
holder.message_stats.setImageResource(temp.message_status);
holder.user_img.setImageResource(temp.user_img);
holder.user_message.setText(temp.user_msg);
return row;
}
}
After changing the layout height of listview to wrap_content I am getting the following error.(App Crashes)
java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.buckydroid.chat.ContactList$viewHolder.user_name' on a null object reference
Upvotes: 1
Views: 82
Reputation: 37414
As per my comment above , you need to use setTag
and getTag
because if else
case executed , your holder
will be null
, hence the exception , so do this
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
viewHolder holder = null;
if (row==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.contact_list_style,parent,false);
holder = new viewHolder(row);
row.setTag(holder);
// ^^^ set the tag
}else{
Toast.makeText(context, "GRRRRRRRRRRR", Toast.LENGTH_SHORT).show();
holder=(viewHolder)row.getTag();
// do this , initialize your holder otherwise holder stays null
}
RowData temp = list.get(position);
holder.user_name.setText(temp.user_name);
holder.message_stats.setImageResource(temp.message_status);
holder.user_img.setImageResource(temp.user_img);
holder.user_message.setText(temp.user_msg);
return row;
}
Note: you can use convertView
instead of row
, no need to create an extra reference variable.
Upvotes: 2
Reputation: 2972
You need to set view holder tag.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
viewHolder holder = null;
if (row==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.contact_list_style,parent,false);
holder = new viewHolder(row);
row .setTag(holder);
}else{
holder = (Holder) row.getTag();
Toast.makeText(context, "GRRRRRRRRRRR", Toast.LENGTH_SHORT).show();
}
RowData temp = list.get(position);
holder.user_name.setText(temp.user_name);
holder.message_stats.setImageResource(temp.message_status);
holder.user_img.setImageResource(temp.user_img);
holder.user_message.setText(temp.user_msg);
return row;
}
Upvotes: 2