2

I have custom Listview.in which data is populates from array ,but I am getting duplicates value in listview while populating listview .please help ...

the below is my code

this is my Adapter 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;
    }
}

this my 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;
   }
}

this is my 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);
    }


}
2
  • 1
    Your source code is terribly formatted -- it's not a pleasure to read. Commented Feb 23, 2015 at 14:52
  • What Lars means is: "please format your code so we can read it and help you". Commented Feb 23, 2015 at 14:53

2 Answers 2

2

Move this piece of code:

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

        holder.tvHome.setText(user.hometown);

Move it as below:

// 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;
Sign up to request clarification or add additional context in comments.

Comments

0

I think you're only setting the data when you create the convertView.

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

should be called not be inside the if (convertView == null) block, but be always executed. So put it behind the else block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.