2013-03-06 73 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

相關問題