2013-03-06 69 views
0

我是新来的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; 
} 
} 

回答

0

,您是否试图使用适配器来更新你的名单?

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

显示和在ListView更新内容,你应该:

  1. 创建或找到自己的ListView
  2. 创建ListAdapter
  3. 添加您ListAdapterListView
  4. 将数据添加到您的ListAdapter
  5. 拨打ListAdapter致电notifyDataSetChanged()

例子:

ListView listView = (ListView) findViewById(R.id.listview); 
MyListAdapter myListAdapter = new MyListAdapter(); 
listView.setAdapter(myListAdapter); 

myListAdapter.add("Hello"); 
myListAdapter.add("Hi"); 
myListAdapter.notifyDataSetChanged(); 

注:如果您使用的ArrayAdapternotifyDataSetChanged()一个子类,直接调用时,你使用的方法add()addAll()

+0

我做正是这一点,除了我传递我的新数据到适配器,然后添加它并调用notifydatasetchanged。我正在使用从基本适配器扩展的自定义适配器...可以用它来做什么?编辑:噢,我使用的是listfragment – HatInACat 2013-03-06 22:34:22

+0

你应该需要重新添加适配器。另外,使用'ListFragment'不应该有任何区别。也许你可以在有机会时添加你的代码。 – nhaarman 2013-03-06 22:38:21

相关问题