如何在 Android Activity 中扩展 ListActivity 其中 AppCompatActivity [英] How to extends ListActivity where AppCompatActivity in Android Activity

查看:35
本文介绍了如何在 Android Activity 中扩展 ListActivity 其中 AppCompatActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Activity extends ListActivity for PullToRefresh.但是我使用了 CustomActionBar 这就是为什么使用 AppCompatActivity.如何解决这个问题.感谢高级

I want to use Activity extends ListActivity for PullToRefresh.But i have use CustomActionBar that's why using AppCompatActivity.How to solve this issue.Thanks in Advanced

public class CustomActionActivity extends ListActivity

public class PullToRefreshActivity extends ListActivity {
    private LinkedList<String> mListItems;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pull_to_refresh);

        // Set a listener to be invoked when the list should be refreshed.
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });

        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mListItems);

        setListAdapter(adapter);
    }

取而代之的是:

public class CustomActionActivity extends AppCompatActivity

推荐答案

如果你想在你的应用中使用 ListView 那么直接使用它而不扩展 ListActivity.像这样

If you want to use ListView in your app then directly use it without extending ListActivity. Like this

public class PullToRefreshActivity extends AppCompatActivity {
private LinkedList<String> mListItems;
PullToRefreshListView listView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pull_to_refresh);
    listView = (PullToRefreshListView) findViewById(R.id.list_view); 
    // Set a listener to be invoked when the list should be refreshed.
    listView.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Do work to refresh the list here.
            new GetDataTask().execute();
        }
    });

    mListItems = new LinkedList<String>();
    mListItems.addAll(Arrays.asList(mStrings));

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, mListItems);

    listView.setAdapter(adapter);
}

这篇关于如何在 Android Activity 中扩展 ListActivity 其中 AppCompatActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆