java 如何在 Android Activity 中扩展 ListActivity 其中 AppCompatActivity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34328235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:45:35  来源:igfitidea点击:

How to extends ListActivity where AppCompatActivity in Android Activity

javaandroidandroid-activityandroid-listview

提问by p. ld

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

我想使用 Activity extends ListActivity for PullToRefresh。但是我使用了 CustomActionBar 这就是为什么使用 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);
    }

Instead of this:

取而代之的是:

public class CustomActionActivity extends AppCompatActivity

采纳答案by Chandrakant Dvivedi

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

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

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);
}

回答by Nigam Patro

As per the Java doucmentationhttps://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.htmlwe can't inherit multiple classes. So, if you want both the functionaloty then use ListViewinside your layout.

根据Java 文档https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html我们不能继承多个类。所以,如果你想要两种功能,那么ListView在你的布局中使用。