sara
sara

Reputation: 81

textView how to display text

How can I display text in TextView at get view after this call

 ListAdapter adapter = new MyCustomAdapter ( 
                      ManageSection.this,  studentList, 
                        R.layout.list_student, new String[] { TAG_StudentID, 
                              TAG_StudentNo,TAG_FullName}, 
                        new int[] { R.id.StudentID, R.id.StudentNo,R.id.FullName}); 
               setListAdapter(adapter); 

and the class MyCustomAdapter has a get view, I will display the text

        holder.FullName= (TextView) convertView.findViewById(R.id.FullName);
        holder.FullName.setText();
        holder.StudentNo=(TextView) convertView.findViewById(R.id.StudentNo);
        holder.StudentNo.setText();

So at set text what should I write cause I do

no=student.get(holder.position).get(TAG_StudentNo);
            name =student.get(holder.position).get(TAG_FullName);

and take no, name but the text is duplicated in the whole list

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


     final ViewHolder holder;
        if(convertView==null)
        {
            LayoutInflater mInflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(resource, parent, false);
            holder = new ViewHolder();
            no=student.get(holder.position).get(TAG_StudentNo);
            name =student.get(holder.position).get(TAG_FullName);
            holder.StudentID= (TextView) convertView.findViewById(R.id.StudentID);
            holder.FullName= (TextView) convertView.findViewById(R.id.FullName);
            holder.FullName.setText();
            holder.StudentNo=(TextView) convertView.findViewById(R.id.StudentNo);
            holder.StudentNo.setText();
            holder.DeleteStudent = (ImageView) convertView.findViewById(R.id.DeleteStudent);
            holder.AlertIcon = (ImageView) convertView.findViewById(R.id.Alert);

         // add a listener for phone call
            holder.DeleteStudent.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                 id = student.get(holder.position).get(TAG_StudentID);
                Toast.makeText(getContext(),id,Toast.LENGTH_LONG).show();


                }

            });


            holder.AlertIcon.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                 //   String email = MyCustomAdapter.listMap.get(holder.position).get("email");
                  //  ActivityHelper.startActivity(ActivityManager.EMAIL, email);
                }

            });

            convertView.setTag(holder);

        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.position = position;
        return convertView;
   }

    private static class ViewHolder
    {
        ImageView DeleteStudent;

        ImageView AlertIcon;

        TextView StudentID, StudentNo ,FullName;

        int position;
    }


}

Can anyone tell me what the problem is here?

Upvotes: 0

Views: 162

Answers (2)

Kailash Dabhi
Kailash Dabhi

Reputation: 3513

its simple buddy U just have to put this line instead of blank setText()

no=student.get(holder.position).get(TAG_StudentNo);// the roll_number string
name =student.get(holder.position).get(TAG_FullName);//full name string
holder.StudentID= (TextView) convertView.findViewById(R.id.StudentID);
holder.FullName= (TextView) convertView.findViewById(R.id.FullName);
holder.FullName.setText(name);//pass fullname
holder.StudentNo=(TextView) convertView.findViewById(R.id.StudentNo);
holder.StudentNo.setText(no);//pass roll no.

you just have to pass the no and 'namevariable tosetText()` method.

Upvotes: 1

Hassan Jawed
Hassan Jawed

Reputation: 1650

Try this code.

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


 final ViewHolder holder;
    if(convertView==null)
    {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(resource, parent, false);
        holder = new ViewHolder();
        convertView.setTag(holder);

    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.position = position;
    no=student.get(holder.position).get(TAG_StudentNo);
    name =student.get(holder.position).get(TAG_FullName);
    holder.StudentID= (TextView) convertView.findViewById(R.id.StudentID);
    holder.FullName= (TextView) convertView.findViewById(R.id.FullName);
    holder.FullName.setText();
    holder.StudentNo=(TextView) convertView.findViewById(R.id.StudentNo);
    holder.StudentNo.setText();
    holder.DeleteStudent = (ImageView) convertView.findViewById(R.id.DeleteStudent);
        holder.AlertIcon = (ImageView) convertView.findViewById(R.id.Alert);

     // add a listener for phone call
        holder.DeleteStudent.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

             id = student.get(holder.position).get(TAG_StudentID);
            Toast.makeText(getContext(),id,Toast.LENGTH_LONG).show();


            }

        });


        holder.AlertIcon.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

             //   String email = MyCustomAdapter.listMap.get(holder.position).get("email");
              //  ActivityHelper.startActivity(ActivityManager.EMAIL, email);
            }

        });



    return convertView;

}

private static class ViewHolder
{
    ImageView DeleteStudent;

    ImageView AlertIcon;

    TextView StudentID, StudentNo ,FullName;

    int position;
}

}

What you were doing is just setting the data only when the convertView is null and if not you just returning the view after some Holder operation. You need to set Text etc, in the textView in every case. If you need more explanation, please ask.

Upvotes: 1

Related Questions