Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让notifyDatasetChanged()使用ListAdapter?_Java_Android_Listview_Android Arrayadapter - Fatal编程技术网

Java 如何让notifyDatasetChanged()使用ListAdapter?

Java 如何让notifyDatasetChanged()使用ListAdapter?,java,android,listview,android-arrayadapter,Java,Android,Listview,Android Arrayadapter,现在我使用setAdapter来更新我的ListView,但我认为正确的方法是使用notifiyDatasetChanged(),我无法在我的主类中使用它(它在适配器中)。以下是错误: 类型ListAdapter的notifyDatasetChanged()方法未定义 我想有更好的方法可以做到这一点——有人能给我指出正确的方向吗 以下是我的代码的相关部分: public class ScoreList extends SherlockFragmentActivity { private

现在我使用setAdapter来更新我的ListView,但我认为正确的方法是使用notifiyDatasetChanged(),我无法在我的主类中使用它(它在适配器中)。以下是错误:

类型ListAdapter的notifyDatasetChanged()方法未定义

我想有更好的方法可以做到这一点——有人能给我指出正确的方向吗

以下是我的代码的相关部分:

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
    }
}
公共类记分表扩展了SherlockFragmentActivity{
私有ListView listViewScore;
静态列表listScore=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.score_列表);
ctx=这个;
listScore=dbh.getAllScores();
listViewScore=(ListView)findViewById(R.id.score\u列表);
setAdapter(新的ScoreListAdapter(ctx、,
R.layout.score_row_item,listScore));
listViewScore.getAdapter().notifyDatasetChanged();//这就是我得到错误的地方
}
}
下面是适配器:

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;
    }   
}
公共类ScoreListAdapter扩展了ArrayAdapter{
私有int资源;
私人充气机;
公共ScoreListAdapter(上下文ctx、int resourceId、列表对象){
超级(ctx、资源ID、对象);
资源=资源ID;
充气机=从(ctx)开始的充气机;
//上下文=ctx;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
convertView=(LinearLayout)充气器。充气(资源,空);
得分=获取项目(位置);
TextView txtName=(TextView)convertView.findViewById(R.id.name);
setText(score.getName());
TextView txtScoreChange=(TextView)convertView
.findViewById(R.id.scoreChange);
int scoreChange=Integer.parseInt(score.getScoreChange());
如果(分数变化>0)
setText(“+”+scoreChange);
否则如果(分数变化<0)
txtScoreChange.setText(“+scoreChange”);
其他的
txtScoreChange.setText(“”);
TextView txtScoreTotal=(TextView)convertView
.findViewById(R.id.scoreTotal);
setxt(score.getScoreTotal());
最终线性布局currentRow=(线性布局)convertView
.findViewById(R.id.scoreRowLayout);
notifyDataSetChanged();
返回视图;
}   
}

不要调用notifyDataSetChanged();方法创建时

仅当listViewScore的内容更改时调用它。。在那个时候使用它-

替换

listView.getAdapter().notifyDatasetChanged();

看看魔法


谢谢。

创建自定义适配器的实例,以便您可以在任何地方使用它

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

它是notifyDataSetChanged()-请注意大写字母s。在显示的行中,小写字母“s”有一个错误。首先,您需要知道notifyDataSetChanged的用法…通知附加的观察者基础数据已更改,反映数据集的任何视图都应刷新自身。在实施之前了解这一点。。。当适配器数据中的数据更新或更改时,将使用NOTIFYDATASETCHANGE()。即与适配器关联的集合已更改。不需要在CreationTanks中调用它,我删掉了很多代码,所以我没有在creation中调用它,只是看起来是这样。谢谢,这是一个非常简单的解释,它工作得非常完美(除了我必须在notifyDataSetChanged()中大写Set;正如其他人所指出的)啊,是的,我只是忽略了它:)但正如我所说,如果您的列表已经加载,则不必在您的情况下使用它。
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(); 
}
}
adapter.notifyDatasetChanged();