我如何获得notifyDatasetChanged()与一个ListAdapter工作? [英] How do I get notifyDatasetChanged() to work with a ListAdapter?
问题描述
现在我用setAdapter更新我的ListView,但我认为正确的方法是使用notifiyDatasetChanged(),我不能得到这在我的主要工作类(它在适配器)。以下是错误:
该方法notifyDatasetChanged()是未定义的类型ListAdapter
我猜有这样做的更好的办法? - 任何人都可以点我在正确的方向
下面是我的code的相关部分:
公共类ScoreList扩展SherlockFragmentActivity { 私人的ListView listViewScore; 静态列表<分值GT; listScore =新的ArrayList<分值GT;();
@覆盖
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.score_list);
CTX =这一点;
listScore = dbh.getAllScores(); listViewScore =(ListView控件)findViewById(R.id.score_list); listViewScore.setAdapter(新ScoreListAdapter(CTX,
R.layout.score_row_item,listScore));
listViewScore.getAdapter()notifyDatasetChanged()。 //这是我得到的错误
}
}
这里的适配器:
公共类ScoreListAdapter扩展ArrayAdapter<分值GT; {
私人诠释的资源;
私人LayoutInflater吹气; 公共ScoreListAdapter(上下文CTX,INT RESOURCEID,列表与LT;分数>对象){
超(CTX,RESOURCEID,对象);
资源= RESOURCEID;
吹气= LayoutInflater.from(CTX);
//语境= CTX;
} @覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){ convertView =(的LinearLayout)inflater.inflate(资源,NULL); 分数分数=的getItem(位置); TextView的txtName的=(TextView中)convertView.findViewById(R.id.name);
txtName.setText(score.getName()); TextView的txtScoreChange =(TextView中)convertView
.findViewById(R.id.scoreChange);
INT scoreChange =的Integer.parseInt(score.getScoreChange());
如果(scoreChange大于0)
txtScoreChange.setText(++ scoreChange);
否则如果(scoreChange℃,)
txtScoreChange.setText(+ scoreChange);
其他
txtScoreChange.setText(); TextView的txtScoreTotal =(TextView中)convertView
.findViewById(R.id.scoreTotal);
txtScoreTotal.setText(score.getScoreTotal()); 最终的LinearLayout currentRow =(LinearLayout中)convertView
.findViewById(R.id.scoreRowLayout); notifyDataSetChanged();
返回convertView;
}
}
创建自定义适配器的一个实例,因此你可以在任何地方使用它,你喜欢...
公共类ScoreList扩展SherlockFragmentActivity {私人的ListView listViewScore;私人ScoreListAdapter适配器;静态列表<分值GT; listScore =新的ArrayList<分值GT;();
@覆盖
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.score_list);
CTX =这一点;
listScore = dbh.getAllScores(); listViewScore =(ListView控件)findViewById(R.id.score_list); 适配器=新ScoreListAdapter(CTX,R.layout.score_row_item,listScore);
listViewScore.setAdapter(适配器);
adapter.notifyDatasetChanged();
}
}
顺便说一句,如果你的listScore阵列已经被加载,那么你就需要使用
adapter.notifyDatasetChanged();
Right now I use setAdapter to update my ListView, but I think the proper way is to use notifiyDatasetChanged() and I can't get that to work in my main class (it's in the adapter). Here is the error:
The method notifyDatasetChanged() is undefined for the type ListAdapter
I'm guessing there is a better way of doing this - can anyone point me in the right direction?
Here's the relevant parts of my code:
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
static List<Score> listScore = new ArrayList<Score>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
listViewScore.setAdapter(new ScoreListAdapter(ctx,
R.layout.score_row_item, listScore));
listViewScore.getAdapter().notifyDatasetChanged(); //this is where I get the error
}
}
Here's the adapter:
public class ScoreListAdapter extends ArrayAdapter<Score> {
private int resource;
private LayoutInflater inflater;
public ScoreListAdapter(Context ctx, int resourceId, List<Score> objects) {
super(ctx, resourceId, objects);
resource = resourceId;
inflater = LayoutInflater.from(ctx);
//context = ctx;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = (LinearLayout) inflater.inflate(resource, null);
Score score = getItem(position);
TextView txtName = (TextView) convertView.findViewById(R.id.name);
txtName.setText(score.getName());
TextView txtScoreChange = (TextView) convertView
.findViewById(R.id.scoreChange);
int scoreChange = Integer.parseInt(score.getScoreChange());
if (scoreChange > 0)
txtScoreChange.setText("+" + scoreChange);
else if (scoreChange < 0)
txtScoreChange.setText("" + scoreChange);
else
txtScoreChange.setText("");
TextView txtScoreTotal = (TextView) convertView
.findViewById(R.id.scoreTotal);
txtScoreTotal.setText(score.getScoreTotal());
final LinearLayout currentRow = (LinearLayout) convertView
.findViewById(R.id.scoreRowLayout);
notifyDataSetChanged();
return convertView;
}
}
Create an instance of your custom adapter, so you can use it anywhere you like...
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
private ScoreListAdapter adapter;
static List<Score> listScore = new ArrayList<Score>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore);
listViewScore.setAdapter(adapter);
adapter.notifyDatasetChanged();
}
}
By the way, if your listScore array is already loaded, then you do not need to use
adapter.notifyDatasetChanged();
这篇关于我如何获得notifyDatasetChanged()与一个ListAdapter工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!