Android 为什么我必须滚动以刷新列表视图?

Android 为什么我必须滚动以刷新列表视图?,android,listview,Android,Listview,我不熟悉安卓系统。我正在尝试更新我的列表视图,我已经尝试了一切…从在ui线程上调用notifydatasetchanged,到只是简单地重新创建我的列表适配器,但是在更新时,无论出于何种原因,无论使用哪种方法,我都必须滚动以查看更改。我的意思是数据更新(比如列表中的13:01更改为13:02),它将更新,但要看到更改,我必须滚动,以便13:01离开屏幕,然后向后移动,它将在视觉上更新。 为什么会这样?(我现在无法发布代码,因为我正在使用手机,但如果需要,我将稍后发布。) 编辑:这是相关的代码……

我不熟悉安卓系统。我正在尝试更新我的列表视图,我已经尝试了一切…从在ui线程上调用notifydatasetchanged,到只是简单地重新创建我的列表适配器,但是在更新时,无论出于何种原因,无论使用哪种方法,我都必须滚动以查看更改。我的意思是数据更新(比如列表中的13:01更改为13:02),它将更新,但要看到更改,我必须滚动,以便13:01离开屏幕,然后向后移动,它将在视觉上更新。 为什么会这样?(我现在无法发布代码,因为我正在使用手机,但如果需要,我将稍后发布。)

编辑:这是相关的代码……对不起,花了这么长时间,我已经好几天没用电脑了

ListFragment的相关部分:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    super.onCreateView(inflater, container, savedInstanceState);

    return inflater.inflate(R.layout.match_fragment, container, false);
}

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    MatchAdapter adapter = (MatchAdapter) this.getListAdapter();

    if(futureMatches)
        adapter = new MatchAdapter (this.getActivity(), ((MainActivity)this.getActivity()).getMatches(), futureMatches);
    else
        adapter = new MatchAdapter (this.getActivity(), ((MainActivity)this.getActivity()).getPastMatches(), futureMatches);

    setListAdapter(adapter);
}

public void refresh()
{
    MatchAdapter adapter;

    //Update array in mainactivity
    if(futureMatches)
        MainActivity.refreshMatches((MainActivity) getActivity());
    else
        MainActivity.refreshPastMatches((MainActivity) getActivity());

    //put updated entries in the adapter
    if(futureMatches)
        adapter = new MatchAdapter (getActivity(), ((MainActivity)getActivity()).getMatches(), futureMatches);
    else
        adapter = new MatchAdapter (getActivity(), ((MainActivity)getActivity()).getPastMatches(), futureMatches);

    setListAdapter(adapter);
    updateList();
}

public void updateList(){

    this.getActivity().runOnUiThread(new Runnable() {

        public void run() {
            ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
            getListView().refreshDrawableState();
            getListView().invalidate();
        }
    });
}

public void onViewStateRestored(Bundle savedInstanceState)
{
    super.onViewStateRestored(savedInstanceState);
}

public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    refresh();
}
我的适配器类:

public class MatchAdapter extends BaseAdapter
{
private final Activity context;
private LayoutInflater inflater;
private boolean time = false;
private boolean futureMatchAdapter = true;
private ArrayList<String> matchList;

public MatchAdapter(Context cont, ArrayList<String> matches, boolean isFutureMatchAdapter)
{
    matchList = matches;
    futureMatchAdapter = isFutureMatchAdapter;
    context = (Activity) cont;
    inflater = LayoutInflater.from(context);
}

public int getCount()
{
    return MatchAdapter.size();
}

@Override
public Object getItem(int position)
{
    return MatchAdapter.get(position);
}

public long getItemId(int position)
{
    return position;
}

public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder;
    String curPos = ""; 
    curPos = MatchAdapter.get(position);

    //times, future matches and past matches are handled differently
    if(curPos.contains("Last updated:"))
        time = true;
    else
        time = false;

    if (convertView == null)
    {
        holder = new ViewHolder();
        if(time)
        {
            convertView = inflater.inflate(R.layout.time_item, null);
            holder.title = (TextView) convertView.findViewById(R.id.item_time);
        }
        else
        {
            if(futureMatchAdapter)
            {
                convertView = inflater.inflate(R.layout.feed_item, null);
                holder.title = (TextView) convertView.findViewById(R.id.item_title);
            }
            else
            {
                convertView = inflater.inflate(R.layout.past_feed_item, null);
                holder.title = (TextView) convertView.findViewById(R.id.item_title_past);
            }
        }

        convertView.setTag(holder);
    }
    else
        holder = (ViewHolder) convertView.getTag();


    if(futureMatchAdapter)
        holder.title.setText(matchList.get(position));
    else
    {
        String matchString = matchList.get(position);

        String alwaysVisible = matchString.replace("<", "vs");
        alwaysVisible = alwaysVisible.replace(">", "vs");

        if(!time)
            alwaysVisible = alwaysVisible.substring(0, alwaysVisible.length() - 1);

        holder.title.setText(alwaysVisible);

        if(matchString.contains(">"))
        {
            String winner = matchString.substring(0, matchString.indexOf(">")) + "won!";
            alwaysVisible = alwaysVisible.concat(winner);
        }
        else if(matchString.contains("<"))
        {
            String winner = matchString.substring(matchString.indexOf("<") + 2, matchString.indexOf("\n")) + " won!";
            alwaysVisible = alwaysVisible.concat(winner);
        }       

        holder.title.setOnClickListener(new pastMatchesOnclickListener(alwaysVisible) 
        {
            public void onClick(View v) 
            {
                ((TextView) v).setText(matchWinner);
            }
        });
    }

    return convertView;
}

static class ViewHolder
{
    TextView title;
    TextView time;
}
}
公共类MatchAdapter扩展了BaseAdapter
{
私人最终活动背景;
私人充气机;
私有布尔时间=false;
私有布尔futureMatchAdapter=true;
私有ArrayList匹配列表;
公共匹配适配器(上下文控制、ArrayList匹配、布尔值isFutureMatchAdapter)
{
匹配列表=匹配项;
futureMatchAdapter=是futureMatchAdapter;
上下文=(活动)续;
充气器=充气器。从(上下文);
}
public int getCount()
{
返回MatchAdapter.size();
}
@凌驾
公共对象getItem(int位置)
{
返回MatchAdapter.get(位置);
}
公共长getItemId(int位置)
{
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图)
{
视窗座;
字符串curPos=“”;
curPos=MatchAdapter.get(位置);
//时间、未来比赛和过去比赛的处理方式不同
如果(curPos.contains(“上次更新:”)
时间=真;
其他的
时间=假;
if(convertView==null)
{
holder=新的ViewHolder();
如果(时间)
{
convertView=充气机。充气(R.layout.time_项,空);
holder.title=(TextView)convertView.findViewById(R.id.item_time);
}
其他的
{
if(futureMatchAdapter)
{
convertView=充气机。充气(R.layout.feed_项,空);
holder.title=(TextView)convertView.findViewById(R.id.item_title);
}
其他的
{
convertView=充气机。充气(R.layout.past\u feed\u item,空);
holder.title=(TextView)convertView.findViewById(R.id.item\u title\u pass);
}
}
convertView.setTag(支架);
}
其他的
holder=(ViewHolder)convertView.getTag();
if(futureMatchAdapter)
holder.title.setText(matchList.get(position));
其他的
{
String matchString=matchList.get(位置);
String alwaysVisible=matchString.replace(“,”vs”);
如果(!时间)
alwaysVisible=alwaysVisible.substring(0,alwaysVisible.length()-1);
holder.title.setText(始终可见);
if(matchString.contains(“>”)
{
String winner=matchString.substring(0,matchString.indexOf(“>”)+“win!”);
alwaysVisible=alwaysVisible.concat(获胜者);
}

else if(matchString.contains)(是否尝试使用适配器更新列表

dataadapter.clear();
dataadapter.addAll(allDataResult);

要在
列表视图中显示和更新内容,您应该:

  • 创建或查找您的
    列表视图
  • 创建您的
    ListAdapter
  • 将您的
    ListAdapter
    添加到您的
    ListView
  • 列表适配器添加数据
  • 列表适配器上调用
    notifyDataSetChanged()
  • 例如:

    ListView listView = (ListView) findViewById(R.id.listview);
    MyListAdapter myListAdapter = new MyListAdapter();
    listView.setAdapter(myListAdapter);
    
    myListAdapter.add("Hello");
    myListAdapter.add("Hi");
    myListAdapter.notifyDataSetChanged();
    

    注意:如果您使用的是
    ArrayAdapter
    的子类,则在使用方法
    add()
    addAll()时,将为您调用
    notifyDataSetChanged()
    等。

    我正是这样做的,除了我将新数据传递给适配器,然后添加数据并调用notifydatasetchanged。我使用的是从基本适配器扩展而来的自定义适配器。这可能与此有关吗?编辑:哦,我使用的是ListFragment您应该需要再次添加适配器。另外,使用
    ListFragment
    不应该有任何区别。也许你可以在有机会的时候添加你的代码。