How to use ActionBarActivity of \"android-support-v7-appcompat\" in the activity which Extends the ListActivity.
For Example I have an Activity
publ
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
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 adapter = new ArrayAdapter(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