Java Android:无法在自定义适配器中更新textView
我对Android(和Java)编程还不熟悉,并且已经设法让一个自定义适配器工作,但是我一直在研究如何更新其中的文本视图。下面您将看到我的score_save函数和我想要的代码,但我不确定如何一次将其应用于适配器的所有行Java Android:无法在自定义适配器中更新textView,java,android,android-listview,custom-adapter,Java,Android,Android Listview,Custom Adapter,我对Android(和Java)编程还不熟悉,并且已经设法让一个自定义适配器工作,但是我一直在研究如何更新其中的文本视图。下面您将看到我的score_save函数和我想要的代码,但我不确定如何一次将其应用于适配器的所有行 public class MainActivity extends Activity { private ListView listView1; private int currentPlayer; private int currentScore; ArrayList<
public class MainActivity extends Activity {
private ListView listView1;
private int currentPlayer;
private int currentScore;
ArrayList<Integer> allScoreChange = new ArrayList<Integer>();
ArrayList<Integer> allScore = new ArrayList<Integer>();
Button button_add, button_add_big, button_sub, button_sub_big,
button_add_num, button_save;
TextView name;
TextView score;
TextView scoreChange;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Score score_data[] = new Score[] { new Score("NameA"),
new Score("NameB"), new Score("NameC"), new Score("NameD"),
new Score("NameE") };
//sets score lists to 0
for (int i = 0; i < 5; i++) {
allScoreChange.add(0);
allScore.add(0);
}
ScoreAdapter adapter = new ScoreAdapter(this,
R.layout.listview_item_row, score_data);
listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
currentScore = allScoreChange.get(position);
// Gets fields from current row
score = (TextView) view.findViewById(R.id.txtScore);
scoreChange = (TextView) view.findViewById(R.id.txtScoreChange);
currentPlayer = position;
}
});
button_save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
score_save();
}
});
}
public void score_save() {
// this needs to go through all rows of listview
currentPlayer = 0;
// update array with new score
allScore.set(currentPlayer, allScore.get(currentPlayer)
+ allScoreChange.get(currentPlayer));
// display new score
score.setText(allScore.get(currentPlayer));
// reset score change to zero
allScoreChange.set(currentPlayer, 0);
// display score change as blank
scoreChange.setText("");
}
}
公共类MainActivity扩展活动{
私有列表视图列表视图1;
私人球员;
私人智力测验成绩;
ArrayList allScoreChange=新建ArrayList();
ArrayList allScore=新的ArrayList();
按钮按钮添加,按钮添加,按钮子,按钮子,
按钮添加数量,按钮保存;
文本视图名称;
文本视图分数;
文本视图更改;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
得分得分_数据[]=新得分[]{new Score(“NameA”),
新分数(“NameB”)、新分数(“NameC”)、新分数(“命名”),
新分数(“姓名”)};
//将分数列表设置为0
对于(int i=0;i<5;i++){
allScoreChange.add(0);
allScore.add(0);
}
ScoreAdapter=新的ScoreAdapter(此,
R.layout.listview(项目行、分数数据);
listView1=(ListView)findViewById(R.id.listView1);
listView1.setAdapter(适配器);
listView1.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
currentScore=allScoreChange.get(位置);
//从当前行获取字段
score=(TextView)view.findViewById(R.id.txtScore);
scoreChange=(TextView)view.findViewById(R.id.txtScoreChange);
当前玩家=位置;
}
});
按钮\u save.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
记分保存();
}
});
}
公共无效分数_save(){
//这需要遍历listview的所有行
currentPlayer=0;
//用新分数更新数组
allScore.set(当前播放器,allScore.get(当前播放器)
+allScoreChange.get(currentPlayer));
//显示新分数
score.setText(allScore.get(currentPlayer));
//将分数重置为零
allScoreChange.set(currentPlayer,0);
//将分数更改显示为空白
scoreChange.setText(“”);
}
}
这是行的XML,我想用allScoreChange和AllScoreArrayList中的值更新txtScoreChange和txtScore。我删掉了定义这些列表的代码,以防您感到奇怪
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="70"
android:text="Name"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtScoreChange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="15"
android:textColor="#ff0000"
android:textSize="22sp"
android:gravity="right" />
<TextView
android:id="@+id/txtScore"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="15"
android:text="1"
android:textColor="#666666"
android:textSize="22sp"
android:gravity="right" />
这是我的适配器:
public class ScoreAdapter extends ArrayAdapter<Score>{
Context context;
int layoutResourceId;
Score data[] = null;
public ScoreAdapter(Context context, int layoutResourceId, Score[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ScoreHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ScoreHolder();
holder.txtScore = (TextView)row.findViewById(R.id.txtScore);
holder.txtScoreChange = (TextView)row.findViewById(R.id.txtScoreChange);
holder.txtName = (TextView)row.findViewById(R.id.txtName);
row.setTag(holder);
}
else
{
holder = (ScoreHolder)row.getTag();
}
Score scoreData = data[position];
holder.txtName.setText(scoreData.name);
holder.txtScore.setText(scoreData.score);
return row;
}
static class ScoreHolder
{
TextView txtScore;
TextView txtName;
TextView txtScoreChange;
}
}
公共类ScoreAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
分数数据[]=null;
公共ScoreAdapter(上下文上下文、int-layoutResourceId、分数[]数据){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
记分人=空;
if(行==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
holder=新计分员();
holder.txtScore=(TextView)row.findViewById(R.id.txtScore);
holder.txtScoreChange=(TextView)row.findViewById(R.id.txtScoreChange);
holder.txtName=(TextView)row.findViewById(R.id.txtName);
row.setTag(支架);
}
其他的
{
holder=(ScoreHolder)row.getTag();
}
得分数据=数据[位置];
holder.txtName.setText(scoreData.name);
holder.txtScore.setText(scoreData.score);
返回行;
}
静态类计分员
{
TextView txtScore;
TextView-txtName;
TextView txtScoreChange;
}
}
调用适配器。notifyDataSetChanged()。这将导致它重新填充listview,在所有可见视图上调用adapter.getView。让getView()为其行设置正确的值。我看到的只是一堆代码,毫无疑问;它告诉我“类型ListView的notifyDataSetChanged()方法未定义”@MichaelG,这是因为notifyDataSetChanged()
是适配器的方法,而不是ListView。谢谢,这是我的问题…太多的“分数”和“适配器”四处浮动。我的错,我会修正我的评论。抢手货