0

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

2 回答 2

0

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

dataadapter.clear();
dataadapter.addAll(allDataResult);
于 2013-03-06T22:10:17.330 回答
0

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

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

例子:

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()等。

于 2013-03-06T22:11:39.990 回答