0

调用后如何TextView在获取视图中显示文本

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

并且类MyCustomAdapter有一个获取视图,我将显示文本

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

所以在设置文本我应该写什么因为我做

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

并取 no, name 但文本在整个列表中重复

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


}

谁能告诉我这里有什么问题?

4

2 回答 2

1

试试这个代码。

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

}

您所做的只是仅在 convertView 为空时设置数据,如果不是,您只需在某些 Holder 操作后返回视图。在每种情况下,您都需要在 textView 中设置 Text 等。如果您需要更多解释,请询问。

于 2012-10-12T10:14:18.363 回答
1

它的简单伙伴你只需要把这一行而不是空白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.

你只需要传递no and 'name variable tosetText()` 方法。

于 2012-10-12T10:21:37.110 回答