为什么我必须滚动才能刷新列表视图?

时间:2013-03-06 22:07:12

标签: android listview

我是android的新手。我正在尝试让我的列表视图更新,我已经尝试了一切......从在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;
}
}

2 个答案:

答案 0 :(得分:0)

您是否尝试使用适配器更新列表?

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

答案 1 :(得分:0)

要显示和更新ListView中的内容,您应该:

  1. 创建或找到您的ListView
  2. 创建ListAdapter
  3. ListAdapter添加到ListView
  4. 将数据添加到ListAdapter
  5. notifyDataSetChanged()
  6. 上致电ListAdapter

    示例:

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

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

相关问题