gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:13:15 25 4
gpt4 key购买 nike

我有自定义 Listview.in,其中数据是从数组填充的,但是我在填充 listview 时在 listview 中得到重复值。请帮助 ...

下面是我的代码

这是我的适配器类

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

这是我的主 Activity

    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;

关于android - 在填充 Listview 时,我在 android 的 listview 中得到重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28676427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com