我有一个自定义ListView,其中每个项目都有3列,我希望通过单击按钮来替换指定项目的文本。
这是代码:
CustomUsersAdapter.java
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
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) {
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.lv_player, parent, false);
}
// Get the data item for this position
User user = getItem(position);
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.name);
TextView tvHome = (TextView) convertView.findViewById(R.id.hometown);
TextView CHid = (TextView) convertView.findViewById(R.id.id);
// Populate the data into the template view using the data object
tvName.setText(user.getName());
tvHome.setText(user.getHometown());
CHid.setText(user.getId());
return convertView;
}
}
User.java
public class User {
//declare private data instead of public to ensure the privacy of data field of each class
private String name;
private String hometown;
private String id;
public User(String name, String hometown, String id ) {
this.name = name;
this.hometown = hometown;
this.id = id;
}
//retrieve user's name
public String getName(){
return name;
}
//retrieve users' hometown
public String getHometown(){
return hometown;
}
//retrieve users' id
public String getId(){
return id;
}
}
MainActivity.java
private ArrayAdapter<String> adapter;
ArrayList<User> users = new ArrayList<User>();
ListView listView = (ListView) findViewById( R.id.lv );
users.add( new User( "Alex", "Geneva", "1") );
users.add( new User( "Paul", "Paris", "2") );
users.add( new User( "Joe", "New-York", "3") );
....
CustomUsersAdapter adapter = new CustomUsersAdapter( this, users );
listView.setAdapter( adapter );
所以我如何替换项目内容?我想通过单击按钮来替换特定项目的名称和家乡。
我尝试了一些代码,例如:
ReplaceItem.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
users[3].add( new User( "Marco", "Barcelona" "3" ) );
or
users.add( new User[3]( "Marco", "Barcelona" "3" ) )
但是很好,这行不通。我对Android Studio不满意,如果答案很简单,请对不起。对不起,我的英语不好。
您的ListView和适配器很好。我认为问题是由您实现Click侦听器的方式引起的。另外,您需要像这样在User.java中添加setter方法:
public class User {
//declare private data instead of public to ensure the privacy of data field of each class
private String name;
private String hometown;
private String id;
public User(String name, String hometown, String id ) {
this.name = name;
this.hometown = hometown;
this.id = id;
}
//retrieve user's name
public String getName(){
return name;
}
//set user's name
public void setName(String name){
this.name = name;
}
//retrieve users' hometown
public String getHometown(){
return hometown;
}
//set user's hometown
public void setHometown(String hometown){
this.hometown= hometown;
}
//retrieve users' id
public String getId(){
return id;
}
//set users' id
public void setId(String id){
this.id = id;
}
}
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//MODFIFY YOUR ARRAY HERE
users.get(position).setName("new name");
users.get(position).setHometown("new hometown");
users.get(position).setId("new id");
adapter.notifyDataSetChanged(); //THIS LINE IS VERY IMPORTANT AFTER YOU ARE DONE MODIFYING
}
});