ActionBarActivity of “android-support-v7-appcompat” and ListActivity in Same activity

前端 未结 6 1622
终归单人心
终归单人心 2020-12-23 15:04

How to use ActionBarActivity of \"android-support-v7-appcompat\" in the activity which Extends the ListActivity.

For Example I have an Activity

publ         


        
相关标签:
6条回答
  • 2020-12-23 15:39

    Add following line of code to your class and make sure that the class implements AdapterView.OnItemClickListener:

    getListView().setOnItemClickListener(this);
    
    0 讨论(0)
  • 2020-12-23 15:51

    my answer based on accepted one, and also contains onListItemClick implementation. But it has a problem with empty view.

    public abstract class ActionBarListActivity extends ActionBarActivity {
    
    private ListView mListView;
    
    protected ListView getListView() {
        if (mListView == null) {
            mListView = (ListView) findViewById(android.R.id.list);
            mListView.setOnItemClickListener(mOnClickListener);
        }
        return mListView;
    }
    
    protected void setListAdapter(ListAdapter adapter) {
        getListView().setAdapter(adapter);
    }
    
    protected ListAdapter getListAdapter() {
        ListAdapter adapter = getListView().getAdapter();
        if (adapter instanceof HeaderViewListAdapter) {
            return ((HeaderViewListAdapter) adapter).getWrappedAdapter();
        } else {
            return adapter;
        }
    }
    
    protected void onListItemClick(ListView l, View v, int position, long id) { }
    
    private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            onListItemClick((ListView) parent, v, position, id);
        }
    };
    }
    
    0 讨论(0)
  • 2020-12-23 16:01

    Maybe you can try to extend ActionBarActivity and for default layout for that activity set some layout that has ListView.

    Something like this:

    public class AlarmListActivity extends ActionBarActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_with_list_view);
    
            ListView lv = (ListView) findViewById(R.id.listView1);
            // populate list view
        }
    }
    

    and correcponding layout file:

    <LinearLayout>
        <ListView
            android:id="@+id/listView1">
        </ListView>
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-23 16:01

    This solution is based on the accepted solution by @patrick. Here is the full code:

    First the XML layout file activity_main.xml. Notice that I have an ListView with ID entryList

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.MainActivity" >
    
        <ListView
            android:id="@+id/entryList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ListView>
    
    </RelativeLayout>
    

    Next is my own ActionBarListActivity. You'd notice some changes. I wanted to make it generic and reusable as possible.

    package com.example.api;
    
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.HeaderViewListAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    
    public abstract class ActionBarListActivity extends ActionBarActivity {
    
        private final class ListOnItemClickListener implements OnItemClickListener {
    
            public void onItemClick(AdapterView<?> lv, View v, int position, long id) {
                onListItemClick((ListView) lv, v, position, id);
                // String str = ((TextView) arg1).getText().toString();
                // Toast.makeText(getBaseContext(), str,
                // Toast.LENGTH_LONG).show();
                // Intent intent = new Intent(getBaseContext(),
                // your_new_Intent.class);
                // intent.putExtra("list_view_value", str);
                // startActivity(intent);
            }
        }
    
        private ListView mListView;
    
        protected ListView getListView() {
    
            if (mListView == null) {
                initListView();
            }
            return mListView;
        }
    
        private void initListView() {
            mListView = (ListView) findViewById(getListViewId());
            if (mListView == null) {
                throw new RuntimeException(
                        "ListView cannot be null. Please set a valid ListViewId");
            }
    
            mListView.setOnItemClickListener(new ListOnItemClickListener());
        }
    
        protected abstract int getListViewId();
    
        protected void setListAdapter(ListAdapter adapter) {
            getListView().setAdapter(adapter);
        }
    
        protected void onListItemClick(ListView lv, View v, int position, long id) {
            // No default functionality. To override
        }
    
        protected ListAdapter getListAdapter() {
            ListAdapter adapter = getListView().getAdapter();
            if (adapter instanceof HeaderViewListAdapter) {
                return ((HeaderViewListAdapter) adapter).getWrappedAdapter();
            } else {
                return adapter;
            }
        }
    }
    

    Next is my MainActivity extending the above class.

    package com.example;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    
    import com.example.api.ActionBarListActivity;
    
    public class MainActivity extends ActionBarListActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                    "Linux", "OS/2" };
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            Log.d("click", "Position click " + position);
            String item = (String) getListAdapter().getItem(position);
            Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
        }
    
        @Override
        protected int getListViewId() {
            return R.id.entryList;
        }
    }
    

    Basically, by overriding onListItemClick() you can say what to do when user accepts something.

    Let me know your thoughts/issues in the comments.

    Cheers

    0 讨论(0)
  • 2020-12-23 16:05

    Instead of creating the layout in XML, I decided to do it in code. The file a is a single drop in replacement in your code.

    package com.stackoverflow.free.examples;
    
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.LinearLayout;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.TableRow;
    import android.widget.TextView;
    
    /**
     * based of https://raw.githubusercontent.com/android/platform_frameworks_base/d6c1919779acb042392615637b9007e0c4b89023/core/java/android/app/ListActivity.java
     * Created by elcuco on 5/27/2014.
     */
    @SuppressWarnings("UnusedDeclaration")
    public class SupportListActivity extends ActionBarActivity {
        protected ListAdapter mAdapter;
        protected ListView mList;
        protected TextView mEmptyMessage;
    
        @Override
        protected void onCreate(Bundle savedBundle)
        {
            super.onCreate(savedBundle);
            mEmptyMessage = new TextView(this);
            mEmptyMessage.setText("No results");
            mList = new ListView(this);
            mList.setEmptyView(mEmptyMessage);
            LinearLayout ll = new LinearLayout(this);
            ll.addView(mEmptyMessage, TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
            ll.addView(mList, TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
            setContentView(ll);
        }
    
        public void setListAdapter(ListAdapter adapter) {
            synchronized (this) {
                mAdapter = adapter;
                mList.setAdapter(adapter);
            }
        }
    
        /**
         * Get the activity's list view widget.
         */
        public ListView getListView() {
            return mList;
        }
    
        /**
         * Set the currently selected list item to the specified
         * position with the adapter's data
         *
         * @param position the position on list to select
         */
        public void setSelection(int position) {
            mList.setSelection(position);
        }
    
        /**
         * Get the position of the currently selected list item.
         */
        public int getSelectedItemPosition() {
            return mList.getSelectedItemPosition();
        }
    
        /**
         * Get the cursor row ID of the currently selected list item.
         */
        public long getSelectedItemId() {
            return mList.getSelectedItemId();
        }
    
        private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id)
            {
                onListItemClick((ListView)parent, v, position, id);
            }
        };
    
        /**
         * This method will be called when an item in the list is selected.
         * Subclasses should override. Subclasses can call
         * getListView().getItemAtPosition(position) if they need to access the
         * data associated with the selected item.
         *
         * @param l The ListView where the click happened
         * @param v The view that was clicked within the ListView
         * @param position The position of the view in the list
         * @param id The row id of the item that was clicked
         */
        protected void onListItemClick(ListView l, View v, int position, long id) {
        }
    }
    
    0 讨论(0)
  • 2020-12-23 16:06

    Here's an implementation of ActionBarListActivity:

    public abstract class ActionBarListActivity extends ActionBarActivity {
    
    private ListView mListView;
    
    protected ListView getListView() {
        if (mListView == null) {
            mListView = (ListView) findViewById(android.R.id.list);
        }
        return mListView;
    }
    
    protected void setListAdapter(ListAdapter adapter) {
        getListView().setAdapter(adapter);
    }
    
    protected ListAdapter getListAdapter() {
        ListAdapter adapter = getListView().getAdapter();
        if (adapter instanceof HeaderViewListAdapter) {
            return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
        } else {
            return adapter;
        }
    }
    }
    

    Just like regular ListActivity, you'll need a layout that contains a ListView with the ID android.R.id.list ("@android:id/list" in XML).

    The spiel in getListAdapter() is to handle cases where header views have been added to the ListView. Seems like ListView sets its own adapter to a HeaderViewListAdapter, so we have to try and get the wrapped adapter to prevent casting errors.

    Edit: Try adding this function to satisfy the need for onListItemClick:

    protected void onListItemClick(ListView lv, View v, int position, long id) {
        getListView().getOnItemClickListener().onItemClick(lv, v, position, id);
    }
    
    0 讨论(0)
提交回复
热议问题