在填充 Listview 时,我在 android 的 listview 中得到重复值

while populating the Listview i am getting duplicates value in listview in android

我有自定义 Listview.in 数据是从数组填充的,但是 我在填充列表视图时在列表视图中得到重复值。请帮忙...

下面是我的代码

这是我的适配器 class

    public class CustomUsersAdapter extends ArrayAdapter<User> 
    {
        public CustomUsersAdapter(Context context, ArrayList<User> users)
    {

            super(context, 0, users);
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    //Get an instance of our cell holder                                                                         
     Holder holder;
     holder = new Holder();

    // Get the data item for this position
      User user = getItem(position);    

    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) 
    {
       convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);

        // Lookup view for data population
        holder.tvName = (TextView) convertView.findViewById(R.id.tvName);

        holder.tvHome = (TextView) convertView.findViewById(R.id.tvHometown);

         holder.tgbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);


    // Populate the data into the template view using the data object
        holder.tvName.setText(user.name);

        holder.tvHome.setText(user.hometown);

        convertView.setTag(holder); //Add this
    }
    else
    {

       holder= (Holder) convertView.getTag();
    }

    /** The clicked Item in the ListView */
    RelativeLayout rLayout = (RelativeLayout) convertView;

    /** Getting the toggle button corresponding to the clicked item */

    final ToggleButton tbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);

    tbtn.setOnClickListener(new OnClickListener() {
        String homet;
        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
             if (tbtn.isChecked()) {

                 tbtn.setChecked(true);

                 ViewGroup parent = (ViewGroup) v.getParent();

                 TextView tvName = (TextView) parent.findViewById(R.id.tvName);

                 homet=tvName.getText().toString();

                Toast.makeText(getContext(),homet+"Blocked", Toast.LENGTH_SHORT).show();
                } else {

                    tbtn.setChecked(false);

                    Toast.makeText(getContext(),homet+ "Unblocked", Toast.LENGTH_SHORT).show();
                 }
        }
        });

    // Return the completed view to render on screen
      return convertView;

        }
            //this holder class will be filled from the layout xml and attached to the row as a tag object


    private class Holder
    {
        TextView tvName;
        TextView tvHome;
        ToggleButton tgbtn,tg1;
    }
}

这是我的User.java

public class User {
public String name;
public String hometown;


  public static String[] call_name= {"shivni","seema","Amar","Swapna","Raj","Ayub","Reshma","Rina","Tina","Rima","Raj"};
public static String[] call_no={"987654","9876543","987654","9873455","655433","45678","56788","78899","45677","654443","5555"};    

  public User(String name, String hometown) {
    this.name = name;
    this.hometown = hometown;

}

public static ArrayList<User> getUsers() {
    ArrayList<User> users = new ArrayList<User>();
    for(int i=0,j=0;i<call_name.length;i++,j++)
    {
    users.add(new User(call_name[i], call_no[j]));
    }
    return users;
   }
}

这是我的 MainActivity

    public class MainActivity extends Activity  {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
    // Attach the adapter to a ListView
                listView = (ListView) findViewById(R.id.lvUsers);
    populateUsersList();

    }

    private void populateUsersList() {
        // Construct the data source
        ArrayList<User> arrayOfUsers = User.getUsers();
        // Create the adapter to convert the array to views
        CustomUsersAdapter adapter = new CustomUsersAdapter(this, arrayOfUsers);


        listView.setAdapter(adapter);
    }


}

移动这段代码:

// Populate the data into the template view using the data object
        holder.tvName.setText(user.name);

        holder.tvHome.setText(user.hometown);

移动如下:

// Populate the data into the template view using the data object
            holder.tvName.setText(user.name);

            holder.tvHome.setText(user.hometown);

/** The clicked Item in the ListView */
    RelativeLayout rLayout = (RelativeLayout) convertView;

我认为您只是在创建 convertView 时才设置数据。

holder.tvName.setText(user.name);
holder.tvHome.setText(user.hometown);

不应在 if (convertView == null) 块内调用,但应始终执行。所以把它放在else块后面。