navigation drawer with pinned (sticky) headers - android

I have a program showing sticky headers in a ListView. I want to apply this to the navigation drawer. Below is the code for PinnedSectionListActivity.java. How to input this ListView in the Navigation Drawer menu?
import com.hb.examples.pinnedsection.R;
public class PinnedSectionListActivity extends ListActivity implements OnClickListener {
static class SimpleAdapter extends ArrayAdapter<Item> implements com.hb.examples.PinnedSectionListView.PinnedSectionListAdapter {
private static final int[] COLORS = new int[] {
R.color.green_light, R.color.orange_light,
R.color.blue_light, R.color.red_light };
public SimpleAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
generateDataset('A', 'Z', false);
}
public void generateDataset(char from, char to, boolean clear) {
if (clear) clear();
final int sectionsNumber = to - from + 1;
prepareSections(sectionsNumber);
int sectionPosition = 0, listPosition = 0;
for (char i=0; i<sectionsNumber; i++) {
Item section = new Item(Item.SECTION, String.valueOf((char)('A' + i)));
section.sectionPosition = sectionPosition;
section.listPosition = listPosition++;
onSectionAdded(section, sectionPosition);
add(section);
final int itemsNumber = (int) Math.abs((Math.cos(2f*Math.PI/3f * sectionsNumber / (i+1f)) * 25f));
for (int j=0;j<itemsNumber;j++) {
Item item = new Item(Item.ITEM, section.text.toUpperCase(Locale.ENGLISH) + " - " + j);
item.sectionPosition = sectionPosition;
item.listPosition = listPosition++;
add(item);
}
sectionPosition++;
}
}
protected void prepareSections(int sectionsNumber) { }
protected void onSectionAdded(Item section, int sectionPosition) { }
#Override public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.DKGRAY);
view.setTag("" + position);
Item item = getItem(position);
if (item.type == Item.SECTION) {
//view.setOnClickListener(PinnedSectionListActivity.this);
view.setBackgroundColor(parent.getResources().getColor(COLORS[item.sectionPosition % COLORS.length]));
}
return view;
}
#Override public int getViewTypeCount() {
return 2;
}
#Override public int getItemViewType(int position) {
return getItem(position).type;
}
#Override
public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}
}
static class FastScrollAdapter extends SimpleAdapter implements SectionIndexer {
private Item[] sections;
public FastScrollAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
#Override protected void prepareSections(int sectionsNumber) {
sections = new Item[sectionsNumber];
}
#Override protected void onSectionAdded(Item section, int sectionPosition) {
sections[sectionPosition] = section;
}
#Override public Item[] getSections() {
return sections;
}
#Override public int getPositionForSection(int section) {
if (section >= sections.length) {
section = sections.length - 1;
}
return sections[section].listPosition;
}
#Override public int getSectionForPosition(int position) {
if (position >= getCount()) {
position = getCount() - 1;
}
return getItem(position).sectionPosition;
}
}
static class Item {
public static final int ITEM = 0;
public static final int SECTION = 1;
public final int type;
public final String text;
public int sectionPosition;
public int listPosition;
public Item(int type, String text) {
this.type = type;
this.text = text;
}
#Override public String toString() {
return text;
}
}
private boolean hasHeaderAndFooter;
private boolean isFastScroll;
private boolean addPadding;
private boolean isShadowVisible = true;
private int mDatasetUpdateCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
isFastScroll = savedInstanceState.getBoolean("isFastScroll");
addPadding = savedInstanceState.getBoolean("addPadding");
isShadowVisible = savedInstanceState.getBoolean("isShadowVisible");
hasHeaderAndFooter = savedInstanceState.getBoolean("hasHeaderAndFooter");
}
initializeHeaderAndFooter();
initializeAdapter();
initializePadding();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isFastScroll", isFastScroll);
outState.putBoolean("addPadding", addPadding);
outState.putBoolean("isShadowVisible", isShadowVisible);
outState.putBoolean("hasHeaderAndFooter", hasHeaderAndFooter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Item item = (Item) getListView().getAdapter().getItem(position);
if (item != null) {
Toast.makeText(this, "Item " + position + ": " + item.text, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Item " + position, Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setChecked(isFastScroll);
menu.getItem(1).setChecked(addPadding);
menu.getItem(2).setChecked(isShadowVisible);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_fastscroll:
isFastScroll = !isFastScroll;
item.setChecked(isFastScroll);
initializeAdapter();
break;
case R.id.action_addpadding:
addPadding = !addPadding;
item.setChecked(addPadding);
initializePadding();
break;
case R.id.action_showShadow:
isShadowVisible = !isShadowVisible;
item.setChecked(isShadowVisible);
((PinnedSectionListView)getListView()).setShadowVisible(isShadowVisible);
break;
case R.id.action_showHeaderAndFooter:
hasHeaderAndFooter = !hasHeaderAndFooter;
item.setChecked(hasHeaderAndFooter);
initializeHeaderAndFooter();
break;
case R.id.action_updateDataset:
updateDataset();
break;
}
return true;
}
private void updateDataset() {
mDatasetUpdateCount++;
SimpleAdapter adapter = (SimpleAdapter) getListAdapter();
switch (mDatasetUpdateCount % 4) {
case 0: adapter.generateDataset('A', 'B', true); break;
case 1: adapter.generateDataset('C', 'M', true); break;
case 2: adapter.generateDataset('P', 'Z', true); break;
case 3: adapter.generateDataset('A', 'Z', true); break;
}
adapter.notifyDataSetChanged();
}
private void initializePadding() {
float density = getResources().getDisplayMetrics().density;
int padding = addPadding ? (int) (16 * density) : 0;
getListView().setPadding(padding, padding, padding, padding);
}
private void initializeHeaderAndFooter() {
setListAdapter(null);
if (hasHeaderAndFooter) {
ListView list = getListView();
LayoutInflater inflater = LayoutInflater.from(this);
TextView header1 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header1.setText("First header");
list.addHeaderView(header1);
TextView header2 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header2.setText("Second header");
list.addHeaderView(header2);
TextView footer = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
footer.setText("Single footer");
list.addFooterView(footer);
}
initializeAdapter();
}
#SuppressLint("NewApi")
private void initializeAdapter() {
getListView().setFastScrollEnabled(isFastScroll);
if (isFastScroll) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getListView().setFastScrollAlwaysVisible(true);
}
setListAdapter(new FastScrollAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
} else {
setListAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
}
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Item: " + v.getTag() , Toast.LENGTH_SHORT).show();
}
}

Related

finding difficulty in setting expandable listView settin OnClick listener

I have created an expandable listview using recyclerView. I am unable to get the correct items in the recyclerView, the item at position 1 an 5 respond to same clicklistener.
ExpandableRecyclerAdapter.java
protected Context mContext;
protected List<T> allItems = new ArrayList<>();
protected List<T> visibleItems = new ArrayList<>();
private List<Integer> indexList = new ArrayList<>();
private SparseIntArray expandMap = new SparseIntArray();
private int mode;
protected static final int TYPE_HEADER = 1000;
private static final int ARROW_ROTATION_DURATION = 150;
public static final int MODE_NORMAL = 0;
public static final int MODE_ACCORDION = 1;
public ExpandableRecyclerAdapter(Context context) {
mContext = context;
}
public static class ListItem
{
public int ItemType;
public ListItem(int itemType)
{
ItemType = itemType;
}
}
#Override
public long getItemId(int i)
{
return i;
}
#Override
public int getItemCount()
{
return visibleItems == null ? 0 : visibleItems.size();
}
protected View inflate(int resourceID, ViewGroup viewGroup)
{
return LayoutInflater.from(mContext).inflate(resourceID, viewGroup, false);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View view) {
super(view);
}
}
public class HeaderViewHolder extends ViewHolder {
ImageView arrow;
public HeaderViewHolder(View view, final ImageView arrow) {
super(view);
this.arrow = arrow;
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handleClick();
}
});
}
protected void handleClick() {
if (toggleExpandedItems(getLayoutPosition(), false)) {
openArrow(arrow);
} else {
closeArrow(arrow);
}
}
public void bind(int position) {
arrow.setRotation(isExpanded(position) ? 90 : 0);
}
}
public boolean toggleExpandedItems(int position, boolean notify) {
if (isExpanded(position)) {
collapseItems(position, notify);
return false;
} else
{
expandItems(position, notify);
if (mode == MODE_ACCORDION)
{
collapseAllExcept(position);
}
return true;
}
}
public void expandItems(int position, boolean notify)
{
int count = 0;
int index = indexList.get(position);
int insert = position;
for (int i=index+1; i<allItems.size() && allItems.get(i).ItemType != TYPE_HEADER; i++)
{
insert++;
count++;
visibleItems.add(insert, allItems.get(i));
indexList.add(insert, i);
}
notifyItemRangeInserted(position + 1, count);
int allItemsPosition = indexList.get(position);
expandMap.put(allItemsPosition, 1);
if (notify)
{
notifyItemChanged(position);
}
}
public void collapseItems(int position, boolean notify)
{
int count = 0;
int index = indexList.get(position);
for (int i=index+1; i<allItems.size() && allItems.get(i).ItemType != TYPE_HEADER; i++) {
count++;
visibleItems.remove(position + 1);
indexList.remove(position + 1);
}
notifyItemRangeRemoved(position + 1, count);
int allItemsPosition = indexList.get(position);
expandMap.delete(allItemsPosition);
if (notify)
{
notifyItemChanged(position);
}
}
public class StaticViewHolder extends ViewHolder {
public StaticViewHolder(View view) {
super(view);
}
}
public class ItemViewHolder extends ViewHolder {
public ItemViewHolder(View view) {
super(view);
}
}
protected boolean isExpanded(int position) {
int allItemsPosition = indexList.get(position);
return expandMap.get(allItemsPosition, -1) >= 0;
}
#Override
public int getItemViewType(int position) {
return visibleItems.get(position).ItemType;
}
public void setItems(List<T> items) {
allItems = items;
List<T> visibleItems = new ArrayList<>();
expandMap.clear();
indexList.clear();
for (int i=0; i<items.size(); i++) {
if (items.get(i).ItemType == TYPE_HEADER) {
indexList.add(i);
visibleItems.add(items.get(i));
}
}
this.visibleItems = visibleItems;
notifyDataSetChanged();
}
protected void notifyItemInserted(int allItemsPosition, int visiblePosition) {
incrementIndexList(allItemsPosition, visiblePosition, 1);
incrementExpandMapAfter(allItemsPosition, 1);
if (visiblePosition >= 0) {
notifyItemInserted(visiblePosition);
}
}
protected void removeItemAt(int visiblePosition) {
int allItemsPosition = indexList.get(visiblePosition);
allItems.remove(allItemsPosition);
visibleItems.remove(visiblePosition);
incrementIndexList(allItemsPosition, visiblePosition, -1);
incrementExpandMapAfter(allItemsPosition, -1);
notifyItemRemoved(visiblePosition);
}
private void incrementExpandMapAfter(int position, int direction) {
SparseIntArray newExpandMap = new SparseIntArray();
for (int i=0; i<expandMap.size(); i++) {
int index = expandMap.keyAt(i);
newExpandMap.put(index < position ? index : index + direction, 1);
}
expandMap = newExpandMap;
}
private void incrementIndexList(int allItemsPosition, int visiblePosition, int direction) {
List<Integer> newIndexList = new ArrayList<>();
for (int i=0; i<indexList.size(); i++) {
if (i == visiblePosition) {
if (direction > 0) {
newIndexList.add(allItemsPosition);
}
}
int val = indexList.get(i);
newIndexList.add(val < allItemsPosition ? val : val + direction);
}
indexList = newIndexList;
}
public void collapseAll() {
collapseAllExcept(-1);
}
public void collapseAllExcept(int position) {
for (int i=visibleItems.size()-1; i>=0; i--) {
if (i != position && getItemViewType(i) == TYPE_HEADER) {
if (isExpanded(i)) {
collapseItems(i, true);
}
}
}
}
public void expandAll() {
for (int i=visibleItems.size()-1; i>=0; i--) {
if (getItemViewType(i) == TYPE_HEADER) {
if (!isExpanded(i))
{
expandItems(i, true);
}
}
}
}
public static void openArrow(View view) {
view.animate().setDuration(ARROW_ROTATION_DURATION).rotation(90);
}
public static void closeArrow(View view) {
view.animate().setDuration(ARROW_ROTATION_DURATION).rotation(0);
}
public int getMode() {
return mode;
}
public void setMode(int mode) {
this.mode = mode;
}
PeopleAdapter.java
public static final int TYPE_PERSON = 1001;
static Activity context;
public PeopleAdapter(Context context) {
super(context);
PeopleAdapter.context = (Activity) context;
setItems(getSampleItems());
}
public static class PeopleListItem extends ExpandableRecyclerAdapter.ListItem {
public String Text;
public PeopleListItem(String group) {
super(TYPE_HEADER);
Text = group;
}
public PeopleListItem(String first, String last) {
super(TYPE_PERSON);
Text = first + " " + last;
}
}
public class HeaderViewHolder extends ExpandableRecyclerAdapter.HeaderViewHolder {
TextView name;
public HeaderViewHolder(View view) {
super(view, (ImageView) view.findViewById(R.id.item_arrow));
name = (TextView) view.findViewById(R.id.item_header_name);
}
public void bind(int position) {
super.bind(position);
name.setText(visibleItems.get(position).Text);
}
}
public class PersonViewHolder extends ExpandableRecyclerAdapter.ViewHolder {
TextView name;
public PersonViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.item_name);
}
public void bind(int position) {
name.setText(visibleItems.get(position).Text);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_HEADER:
return new HeaderViewHolder(inflate(R.layout.item_header, parent));
case TYPE_PERSON:
default:
return new PersonViewHolder(inflate(R.layout.item_person, parent));
}
}
#Override
public void onBindViewHolder(ExpandableRecyclerAdapter.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case TYPE_HEADER:
((HeaderViewHolder) holder).bind(position);
break;
case TYPE_PERSON:
default:
((PersonViewHolder) holder).bind(position);
break;
}
}
private List<PeopleListItem> getSampleItems() {
List<PeopleListItem> items = new ArrayList<>();
items.add(new PeopleListItem("Friends"));
items.add(new PeopleListItem("Bill", "Smith"));
items.add(new PeopleListItem("John", "Doe"));
items.add(new PeopleListItem("Frank", "Hall"));
items.add(new PeopleListItem("Sue", "West"));
items.add(new PeopleListItem("Family"));
items.add(new PeopleListItem("Drew", "Smith"));
items.add(new PeopleListItem("Chris", "Doe"));
items.add(new PeopleListItem("Alex", "Hall"));
items.add(new PeopleListItem("Alex", "Hall"));
items.add(new PeopleListItem("Alex", "Hall"));
items.add(new PeopleListItem("Associates"));
items.add(new PeopleListItem("John", "Jones"));
items.add(new PeopleListItem("Ed", "Smith"));
items.add(new PeopleListItem("Jane", "Hall"));
items.add(new PeopleListItem("Tim", "Lake"));
items.add(new PeopleListItem("Colleagues"));
items.add(new PeopleListItem("Carol", "Jones"));
items.add(new PeopleListItem("Alex", "Smith"));
items.add(new PeopleListItem("Kristin", "Hall"));
items.add(new PeopleListItem("Pete", "Lake"));
return items;
}
i have also created a recyclerView clicklistener
MainActivity.java
recycler.setLayoutManager(new LinearLayoutManager(this));
recycler.addOnItemTouchListener(
new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
// TODO Handle item click
Log.e("#####", "" + position);
switch (position)
{
case 0:
recycler.getFocusedChild();
Toast.makeText(getApplicationContext(), (CharSequence) recycler.getFocusedChild(),
Toast.LENGTH_SHORT).show();
break;
case 2:
Intent in=new Intent(getApplicationContext(),sample.class);
startActivity(in);
break;
case 7:
Toast.makeText(getApplicationContext(),"7",Toast.LENGTH_SHORT).show();
break;
}
}
})
);
recycler.setAdapter(adapter);
Expand you're bind method with a setOnClickListener.
public void bind(int position) {
name.setText(visibleItems.get(position).Text);
name.setOnClickListener(new View.OnClickListener()
#Override
public void onClick(View v) {
doSomething(visibleItems.get(position));
}
});
}

Using onclicklistener in a Fragmentactivity?

i have prepared an onclicklistener listview to put in a tab menu but after all done, i have noticed that such thing is not possible, i dont know may be with a small change to the code i can achieve what i need but my brain seriously stopped due to fraustrating:( :
public class MainActivity extends FragmentActivity {
private final Handler handler = new Handler();
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
private Drawable oldBackground = null;
private int currentColor = 0xFF666666;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
final int pageMargin = (int)
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
changeColor(currentColor);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_contact:
QuickContactFragment dialog = new QuickContactFragment();
dialog.show(getSupportFragmentManager(), "QuickContactFragment");
return true;
}
return super.onOptionsItemSelected(item);
}
private void changeColor(int newColor) {
tabs.setIndicatorColor(newColor);
// change ActionBar color just if an ActionBar is available
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Drawable colorDrawable = new ColorDrawable(newColor);
Drawable bottomDrawable =
getResources().getDrawable(R.drawable.actionbar_bottom);
LayerDrawable ld = new LayerDrawable(new Drawable[] {
colorDrawable, bottomDrawable });
if (oldBackground == null) {
if (Build.VERSION.SDK_INT <
Build.VERSION_CODES.JELLY_BEAN_MR1) {
ld.setCallback(drawableCallback);
} else {
getActionBar().setBackgroundDrawable(ld);
}
} else {
TransitionDrawable td = new TransitionDrawable(new
Drawable[] { oldBackground, ld });
// workaround for broken ActionBarContainer drawable
handling on
// pre-API 17 builds
// https://github.com/android/platform_frameworks_base
/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4
if (Build.VERSION.SDK_INT <
Build.VERSION_CODES.JELLY_BEAN_MR1) {
td.setCallback(drawableCallback);
} else {
getActionBar().setBackgroundDrawable(td);
}
td.startTransition(200);
}
oldBackground = ld;
// http://stackoverflow.com/questions/11002691/actionbar-
setbackgrounddrawable-nulling-background-from-thread-handler
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowTitleEnabled(true);
}
currentColor = newColor;
}
public void onColorClicked(View v) {
int color = Color.parseColor(v.getTag().toString());
changeColor(color);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentColor", currentColor);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
currentColor = savedInstanceState.getInt("currentColor");
changeColor(currentColor);
}
private Drawable.Callback drawableCallback = new Drawable.Callback() {
#Override
public void invalidateDrawable(Drawable who) {
getActionBar().setBackgroundDrawable(who);
}
#Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
handler.postAtTime(what, when);
}
#Override
public void unscheduleDrawable(Drawable who, Runnable what) {
handler.removeCallbacks(what);
}
};
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Categories", "Home", "Top Paid" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new TopRatedFragment();
case 1:
// Games fragment activity
return new GamesFragment();
case 2:
// Movies fragment activity
return new MoviesFragment();
}
return null;
}
}
}
here my beloved listview :
public class PinnedSectionListActivity extends ListActivity implements
OnClickListener {
static class SimpleAdapter extends ArrayAdapter<Item> implements
PinnedSectionListAdapter {
private static final int[] COLORS = new int[] {
android.R.color.holo_green_light, android.R.color.holo_orange_dark,
android.R.color.holo_blue_light, android.R.color.holo_red_light };
public SimpleAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
final int sectionsNumber = 'Z' - 'A' + 1;
prepareSections(sectionsNumber);
int sectionPosition = 0, listPosition = 0;
for (char i=0; i<sectionsNumber; i++) {
Item section = new Item(Item.SECTION, String.valueOf((char)('A' + i)));
section.sectionPosition = sectionPosition;
section.listPosition = listPosition++;
onSectionAdded(section, sectionPosition);
add(section);
final int itemsNumber = (int) Math.abs((Math.cos(2f*Math.PI/3f *
sectionsNumber / (i+1f)) * 25f));
for (int j=0;j<itemsNumber;j++) {
Item item = new Item(Item.ITEM,
section.text.toUpperCase(Locale.ENGLISH) + " - " + j);
item.sectionPosition = sectionPosition;
item.listPosition = listPosition++;
add(item);
}
sectionPosition++;
}
}
protected void prepareSections(int sectionsNumber) { }
protected void onSectionAdded(Item section, int sectionPosition) { }
#Override public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.DKGRAY);
view.setTag("" + position);
Item item = getItem(position);
if (item.type == Item.SECTION) {
//view.setOnClickListener(PinnedSectionListActivity.this);
view.setBackgroundColor(parent.getResources().getColor(COLORS[item.sectionPosition %
COLORS.length]));
}
return view;
}
#Override public int getViewTypeCount() {
return 2;
}
#Override public int getItemViewType(int position) {
return getItem(position).type;
}
#Override
public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}
}
static class FastScrollAdapter extends SimpleAdapter implements SectionIndexer {
private Item[] sections;
public FastScrollAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
#Override protected void prepareSections(int sectionsNumber) {
sections = new Item[sectionsNumber];
}
#Override protected void onSectionAdded(Item section, int sectionPosition) {
sections[sectionPosition] = section;
}
#Override public Item[] getSections() {
return sections;
}
#Override public int getPositionForSection(int section) {
if (section >= sections.length) {
section = sections.length - 1;
}
return sections[section].listPosition;
}
#Override public int getSectionForPosition(int position) {
if (position >= getCount()) {
position = getCount() - 1;
}
return getItem(position).sectionPosition;
}
}
static class Item {
public static final int ITEM = 0;
public static final int SECTION = 1;
public final int type;
public final String text;
public int sectionPosition;
public int listPosition;
public Item(int type, String text) {
this.type = type;
this.text = text;
}
#Override public String toString() {
return text;
}
}
private boolean hasHeaderAndFooter;
private boolean isFastScroll;
private boolean addPadding;
private boolean isShadowVisible = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
if (savedInstanceState != null) {
isFastScroll = savedInstanceState.getBoolean("isFastScroll");
addPadding = savedInstanceState.getBoolean("addPadding");
isShadowVisible = savedInstanceState.getBoolean("isShadowVisible");
hasHeaderAndFooter =
savedInstanceState.getBoolean("hasHeaderAndFooter");
}
initializeHeaderAndFooter();
initializeAdapter();
initializePadding();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isFastScroll", isFastScroll);
outState.putBoolean("addPadding", addPadding);
outState.putBoolean("isShadowVisible", isShadowVisible);
outState.putBoolean("hasHeaderAndFooter", hasHeaderAndFooter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Item item = (Item) getListView().getAdapter().getItem(position);
if (item != null) {
Toast.makeText(this, "Item " + position + ": " + item.text,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Item " + position, Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setChecked(isFastScroll);
menu.getItem(1).setChecked(addPadding);
menu.getItem(2).setChecked(isShadowVisible);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_fastscroll:
isFastScroll = !isFastScroll;
item.setChecked(isFastScroll);
initializeAdapter();
break;
case R.id.action_addpadding:
addPadding = !addPadding;
item.setChecked(addPadding);
initializePadding();
break;
case R.id.action_showShadow:
isShadowVisible = !isShadowVisible;
item.setChecked(isShadowVisible);
((PinnedSectionListView)getListView()).setShadowVisible(isShadowVisible);
break;
case R.id.action_showHeaderAndFooter:
hasHeaderAndFooter = !hasHeaderAndFooter;
item.setChecked(hasHeaderAndFooter);
initializeHeaderAndFooter();
break;
}
return true;
}
private void initializePadding() {
float density = getResources().getDisplayMetrics().density;
int padding = addPadding ? (int) (16 * density) : 0;
getListView().setPadding(padding, padding, padding, padding);
}
private void initializeHeaderAndFooter() {
setListAdapter(null);
if (hasHeaderAndFooter) {
ListView list = getListView();
LayoutInflater inflater = LayoutInflater.from(this);
TextView header1 = (TextView)
inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header1.setText("First header");
list.addHeaderView(header1);
TextView header2 = (TextView)
inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header2.setText("Second header");
list.addHeaderView(header2);
TextView footer = (TextView)
inflater.inflate(android.R.layout.simple_list_item_1, list, false);
footer.setText("Single footer");
list.addFooterView(footer);
}
initializeAdapter();
}
#SuppressLint("NewApi")
private void initializeAdapter() {
getListView().setFastScrollEnabled(isFastScroll);
if (isFastScroll) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getListView().setFastScrollAlwaysVisible(true);
}
setListAdapter(new FastScrollAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1));
} else {
setListAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1,
android.R.id.text1));
}
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Item: " + v.getTag() , Toast.LENGTH_SHORT).show();
}
}
i think what you need is navigation drawer
or you can use action bar

Converting ListActivity to ListFragment

How can I go about changing this ListActivity to a ListFragment..? I have looked on the web for some resources that would help and have tried myself but to no avail. Does ListFragment have same methods as ListActivity..? Please help. Thank you all in advance.
Here is my Code:
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;
import com.hb.examples.pinnedsection.R;
import com.hb.views.PinnedSectionListView;
import com.hb.views.PinnedSectionListView.PinnedSectionListAdapter;
public class SectionedListActivity extends ListActivity implements OnClickListener {
static class SimpleAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter {
private static final int[] COLORS = new int[] {
R.color.green_light, R.color.orange_light,
R.color.blue_light, R.color.red_light };
public SimpleAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
final int sectionsNumber = 'Z' - 'A' + 1;
prepareSections(sectionsNumber);
int sectionPosition = 0, listPosition = 0;
for (char i=0; i<sectionsNumber; i++) {
Item section = new Item(Item.SECTION, String.valueOf((char)('A' + i)));
section.sectionPosition = sectionPosition;
section.listPosition = listPosition++;
onSectionAdded(section, sectionPosition);
add(section);
final int itemsNumber = (int) Math.abs((Math.cos(2f*Math.PI/3f * sectionsNumber / (i+1f)) * 25f));
for (int j=0;j<itemsNumber;j++) {
Item item = new Item(Item.ITEM, section.text.toUpperCase(Locale.ENGLISH) + " - " + j);
item.sectionPosition = sectionPosition;
item.listPosition = listPosition++;
add(item);
}
sectionPosition++;
}
}
protected void prepareSections(int sectionsNumber) { }
protected void onSectionAdded(Item section, int sectionPosition) { }
#Override public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.DKGRAY);
view.setTag("" + position);
Item item = getItem(position);
if (item.type == Item.SECTION) {
//view.setOnClickListener(SectionedListActivity.this);
view.setBackgroundColor(parent.getResources().getColor(COLORS[item.sectionPosition % COLORS.length]));
}
return view;
}
#Override public int getViewTypeCount() {
return 2;
}
#Override public int getItemViewType(int position) {
return getItem(position).type;
}
#Override
public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}
}
static class FastScrollAdapter extends SimpleAdapter implements SectionIndexer {
private Item[] sections;
public FastScrollAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
#Override protected void prepareSections(int sectionsNumber) {
sections = new Item[sectionsNumber];
}
#Override protected void onSectionAdded(Item section, int sectionPosition) {
sections[sectionPosition] = section;
}
#Override public Item[] getSections() {
return sections;
}
#Override public int getPositionForSection(int section) {
if (section >= sections.length) {
section = sections.length - 1;
}
return sections[section].listPosition;
}
#Override public int getSectionForPosition(int position) {
if (position >= getCount()) {
position = getCount() - 1;
}
return getItem(position).sectionPosition;
}
}
static class Item {
public static final int ITEM = 0;
public static final int SECTION = 1;
public final int type;
public final String text;
public int sectionPosition;
public int listPosition;
public Item(int type, String text) {
this.type = type;
this.text = text;
}
#Override public String toString() {
return text;
}
}
private boolean hasHeaderAndFooter;
private boolean isFastScroll;
private boolean addPadding;
private boolean isShadowVisible = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
isFastScroll = savedInstanceState.getBoolean("isFastScroll");
addPadding = savedInstanceState.getBoolean("addPadding");
isShadowVisible = savedInstanceState.getBoolean("isShadowVisible");
hasHeaderAndFooter = savedInstanceState.getBoolean("hasHeaderAndFooter");
}
initializeHeaderAndFooter();
initializeAdapter();
initializePadding();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isFastScroll", isFastScroll);
outState.putBoolean("addPadding", addPadding);
outState.putBoolean("isShadowVisible", isShadowVisible);
outState.putBoolean("hasHeaderAndFooter", hasHeaderAndFooter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Item item = (Item) getListView().getAdapter().getItem(position);
if (item != null) {
Toast.makeText(this, "Item " + position + ": " + item.text, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Item " + position, Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setChecked(isFastScroll);
menu.getItem(1).setChecked(addPadding);
menu.getItem(2).setChecked(isShadowVisible);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_fastscroll:
isFastScroll = !isFastScroll;
item.setChecked(isFastScroll);
initializeAdapter();
break;
case R.id.action_addpadding:
addPadding = !addPadding;
item.setChecked(addPadding);
initializePadding();
break;
case R.id.action_showShadow:
isShadowVisible = !isShadowVisible;
item.setChecked(isShadowVisible);
((PinnedSectionListView)getListView()).setShadowVisible(isShadowVisible);
break;
case R.id.action_showHeaderAndFooter:
hasHeaderAndFooter = !hasHeaderAndFooter;
item.setChecked(hasHeaderAndFooter);
initializeHeaderAndFooter();
break;
}
return true;
}
private void initializePadding() {
float density = getResources().getDisplayMetrics().density;
int padding = addPadding ? (int) (16 * density) : 0;
getListView().setPadding(padding, padding, padding, padding);
}
private void initializeHeaderAndFooter() {
setListAdapter(null);
if (hasHeaderAndFooter) {
ListView list = getListView();
LayoutInflater inflater = LayoutInflater.from(this);
TextView header1 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header1.setText("First header");
list.addHeaderView(header1);
TextView header2 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header2.setText("Second header");
list.addHeaderView(header2);
TextView footer = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
footer.setText("Single footer");
list.addFooterView(footer);
}
initializeAdapter();
}
#SuppressLint("NewApi")
private void initializeAdapter() {
getListView().setFastScrollEnabled(isFastScroll);
if (isFastScroll) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getListView().setFastScrollAlwaysVisible(true);
}
setListAdapter(new FastScrollAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
} else {
setListAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
}
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Item: " + v.getTag() , Toast.LENGTH_SHORT).show();
}
}
I was able to find a way to convert to ListFragment
Code:
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
public static class PinnedListView extends ListFragment implements OnClickListener {
private boolean hasHeaderAndFooter;
private boolean isFastScroll;
private boolean addPadding;
private boolean isShadowVisible = true;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.custom_view,container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
isFastScroll = savedInstanceState.getBoolean("isFastScroll");
addPadding = savedInstanceState.getBoolean("addPadding");
isShadowVisible = savedInstanceState.getBoolean("isShadowVisible");
hasHeaderAndFooter = savedInstanceState.getBoolean("hasHeaderAndFooter");
}
initializeHeaderAndFooter();
initializeAdapter();
initializePadding();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isFastScroll", isFastScroll);
outState.putBoolean("addPadding", addPadding);
outState.putBoolean("isShadowVisible", isShadowVisible);
outState.putBoolean("hasHeaderAndFooter", hasHeaderAndFooter);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Item item = (Item) getListView().getAdapter().getItem(position);
if (item != null) {
Toast.makeText(getActivity(), "Item " + position + ": " + item.text, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Item " + position, Toast.LENGTH_SHORT).show();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getActivity().getMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setChecked(isFastScroll);
menu.getItem(1).setChecked(addPadding);
menu.getItem(2).setChecked(isShadowVisible);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_fastscroll:
isFastScroll = !isFastScroll;
item.setChecked(isFastScroll);
initializeAdapter();
break;
case R.id.action_addpadding:
addPadding = !addPadding;
item.setChecked(addPadding);
initializePadding();
break;
case R.id.action_showShadow:
isShadowVisible = !isShadowVisible;
item.setChecked(isShadowVisible);
((PinnedSectionListView)getListView()).setShadowVisible(isShadowVisible);
break;
case R.id.action_showHeaderAndFooter:
hasHeaderAndFooter = !hasHeaderAndFooter;
item.setChecked(hasHeaderAndFooter);
initializeHeaderAndFooter();
break;
}
return true;
}
private void initializePadding() {
float density = getResources().getDisplayMetrics().density;
int padding = addPadding ? (int) (16 * density) : 0;
getListView().setPadding(padding, padding, padding, padding);
}
private void initializeHeaderAndFooter() {
setListAdapter(null);
if (hasHeaderAndFooter) {
ListView list = getListView();
LayoutInflater inflater = LayoutInflater.from(getActivity());
TextView header1 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header1.setText("First header");
list.addHeaderView(header1);
TextView header2 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header2.setText("Second header");
list.addHeaderView(header2);
TextView footer = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
footer.setText("Single footer");
list.addFooterView(footer);
}
initializeAdapter();
}
#SuppressLint("NewApi")
private void initializeAdapter() {
getListView().setFastScrollEnabled(isFastScroll);
if (isFastScroll) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getListView().setFastScrollAlwaysVisible(true);
}
setListAdapter(new FastScrollAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1));
} else {
setListAdapter(new SimpleAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1));
}
}
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Item: " + v.getTag() , Toast.LENGTH_SHORT).show();
}
}
ListFragment will provide you with all what you need like ListActivity. You need to make a new Activity (Main Activity) extends FragmentActivity and make the ListFragment as a different class then add the Fragment to the Activity. If you don't know how to do this I strongly recommend you read The Fragments Tutorial Here.

Calling a ListActivity within a Fragment

I would like to know if and all If it is possible to call a list activity within an activity. I've spent the greater part of two days trying to make it work. But have fumbled over and over. Please help. Thanks in Advance.
public static class GreetFrag extends Fragment {
public static final String ARG_OBJECT = "object";
private List<RowItem> rowItems;
private List<Item> newList;
ListView lv;
ListView newLv;
Item item;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_greetfrag_swipeview, container, false);
//lv = (ListView) rootView.findViewById(R.id.myList);
newLv = (ListView) rootView.findViewById(R.id.newList);
//rowItems = new ArrayList<RowItem>();
newList = new ArrayList<MenuActivity.GreetFrag.PinnedSectionListActivity.Item>();
String[] english = {"Hi (to same age or younger)",
"Yo!", "Hey!", "Good to see ya.","Hello?","Who dis?","What’s up?","How ya been?","Yeah, I’m good!","Good!","So-so.","Sucky.",
"Whatcha been up to?","Long time no see!","It’s been a while.","You’re still alive?","You need to lose some weight!",
"Your face has gotten chubbier!","Your mom has gotten chubbier!","Goodbye","Goodbye (general use)","Goodbye (to a person staying)",
"Goodbye (to a person leaving)","Goodbye (when both parties are parting)","Bye bye","See ya!","Later!","I’m out.","Be careful!",
"Good morning(formal)", "G'morning!", "Sleep well?", "It's a beautiful evening!", "Good evening!(formal)", "Evenin", "Sweet dreams!",
"Dream about me!", "Sorry!(informal)", "FOrgive me!", "Oops!", "My bad!", "No worries", "It's all good", "No problem", "Forget aboutit",
"Can you please do me a favor?", "Please, accept my heart!", "Excuse me", "Move our of my way!", "Be careful!", "My name is Michael",
"I am from America", "I have yello fever", "I am 18 years ola dn full of testosterone", "I like hairy girls",
"Can you setme up on a blind date with one of your hottie frineds?", "Nice to meet you", "What's your name?",
"Have we met before?", "Do you have the time?", "How old are you?"};
String[] roman = {"Annyeong",
"Ya!", "Imma!", "Banga banga","Yeoboseyo?","Nuguseyo?","Wannya?","Jal jinaeni?","Eung, jal jinae!","Joa!","Geunyang geurae.","Guryeo.",
"Mohago sarannya?","Oretmaniyeyo!","Olman!","Saraisseonnya?","Sal jom bbaeyagetda!",
"Eolguli tongtonghae!", "Neone eomma saljom jjisyeosseo!","Jal jinae~","Annyeong~","Jal isseo~","Tto boja!",
"Jal ga~","Jal jinae~","Bai bai","Najungebwa!","Na ganda.","Josimhae!", "Annyeonghi jumusyeoseoyo","Joeun achim!",
"Jal Jasseo?","Joeun bam!", "Anneonghi jumuseyo", "jalja", "Joeun kkumkkwo!", "Nae kkumkkwo!", "Mian!",
"Yongseohaejwo!", "At!", "Silsu!", "Geokjeonghajima", "Da gwaenchana", "Munjae eopsseo", "Ijeobeoryeo",
"Budi butak jom deureojullae?", "Jebal je mamaeul badajuseyo!", "Sillyehapnida/Jamsimanyo", "Bikyeo!",
"Josim!", "Je ireumeun Michael ipsida", "Jeoneun migukeseo wasseupnida", "Jeoneun dongyanginman joahapnida",
"Jeoneun yeolyeodeolssaligo himi neomchipnida", "Jeoneun teol maneun yeojaga jossepnida",
"Jal sang-gin chingurang na sogaeting sikyejo?", "Mannaseo ban-gapseupnida", "Ireumi mwoyeyo?",
"Uri mannanjeok itnayo?", "Jgeum myeossiyeyo?", "Naiga eotteoke dwae?"};
String[] hangul = {"안녕",
"야!", "임마!", "방가 방가","여보세요?","누구세요?","왔냐?","잘 지내니?","응, 잘 지내!","좋아!","그냥 그래.","구려.",
"뭐하고 살았냐?","오랫만이예요!","올만!","살아있었냐?","살 좀 빼야겠다!",
"얼굴이 통통해!","너네 엄마 살좀 찌셨어!","잘 지내~","안녕~","잘 있어~",
"잘 가~","잘 지내~","바이 바이","또 보자!","나중에봐!","나 간다.","조심해!",
"안녕히 주무셨어요", "좋은 아침!", "잘 잤어?", "좋은 밤!", "안녕히 주무세요",
"잘자", "좋은 꿈꿔!", "내 꿈꿔!", "미안!", "용서해줘!", "앗!", "실수!", "걱정하지마",
"다 괜찮아","문제없어", "잊어버려", "부디 부탁 좀 들어줄래?", "제발 제 맘을 받아주세요!",
"실례합니다/잠시만요", "비켜!", "조심!", "제 이름은 마이클입니다", "저는 미국에서 왔습니다",
"저는 동양인만 좋아합니다", "저는 열여덟살이고 힘이 넘칩니다", "저는 털 많은 여자가 좋습니다",
"잘 생긴 친구랑 나 소개팅 시켜줘?", "만나서 반갑습니다", "이름이 뭐예요?", "우리 만난적 있나요?",
"지금 몇시예요?", "나이가 어떻게 돼?"};
//Populate the List
for (int i = 0; i < english.length; i++) {
item = new Item(english[i], hangul[i], roman[i]);
newList.add(item);
}
// Set the adapter on the ListView
LazyAdapter adapter = new LazyAdapter(getActivity(), R.layout.view_lessons, rowItems);
//lv.setAdapter(adapter);
/**
lv.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
**/
return rootView;
}
public class PinnedSectionListActivity extends ListActivity implements OnClickListener {
public class CustomAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter {
Context context;
List <Item> items;
private final int[] COLORS = new int[] {
R.color.green_light,
R.color.orange_light,
R.color.blue_light,
R.color.red_light };
class ViewHolder {
int sectionPosition;
int listPosition;
TextView english;
TextView romanization;
TextView hangul;
RelativeLayout card;
}
public CustomAdapter(Context context, int resourceId, List<Item> items) {
super(context, resourceId, items);
this.context = context;
}
public CustomAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
final int sectionsNumber = 'Z' - 'A' + 1;
prepareSections(sectionsNumber);
int sectionPosition = 0, listPosition = 0;
for (int i=0; i< sectionsNumber; i++) {
String title = null;
final String []country = {
"Korean", "Japanese", "Chinese", "Cambodian", "Loas", "Taiwamese"
};
final String [] CATEGORY = {
"Language",
"sports",
"love",
"luxury",
"vacation",
"games",
"home",
"travel",
"electronics",
"movies",
};
switch (('A' + i)) {
case ('A' + 0):
title = country[0];
break;
case ('A' + 1):
title = country[1];
break;
case ('A' + 2):
title = country[2];
break;
case ('A' + 3):
title = country[3];
break;
case ('A' + 4):
title = country[4];
break;
case ('A' + 5):
title = country[5];
break;
default:
break;
}
//Create a new Item class with section header and Name
Item section = new Item(Item.SECTION, title + " " + i);
//Item section = new Item(Item.SECTION, String.valueOf((char)('A' + i)));
section.sectionPosition = sectionPosition;
section.listPosition = listPosition++;
onSectionAdded(section, sectionPosition);
add(section);
final int itemsNumber = CATEGORY.length;
//(int) Math.abs((Math.cos(2f*Math.PI/3f * sectionsNumber / (i+1f)) * 25f));
// For loop to iterate the exact number of itemNumber
for (int j = 0;j < CATEGORY.length;j++) {
//Item item = new Item(Item.ITEM, section.text.toUpperCase(Locale.KOREA) + " - " + j);
Item item = new Item(Item.ITEM, CATEGORY[j]);
item.sectionPosition = sectionPosition;
item.listPosition = listPosition++;
add(item);
}
sectionPosition++;
}
}
protected void prepareSections(int sectionsNumber) { }
protected void onSectionAdded(Item section, int sectionPosition) { }
#Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view = null;
Item item = getItem(position);
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.view_lessons, null);
view.card = (RelativeLayout) convertView.findViewById(R.id.card);
view.romanization = (TextView)convertView.findViewById(R.id.romanization);
view.english = (TextView)convertView.findViewById(R.id.english_translation);
view.hangul = (TextView)convertView.findViewById(R.id.hangul);
convertView.setTag(view);
} else {
view = (ViewHolder)convertView.getTag();
view.romanization.setText(item.getRomanization());
view.english.setText(item.getEnglish());
view.hangul.setText(item.getHangul());
Animation animation = AnimationUtils.loadAnimation(context, R.anim.card_animation);
view.card.startAnimation(animation);
if (item.type == Item.SECTION) {
//view.setOnClickListener(PinnedSectionListActivity.this);
view.english.setBackgroundColor(parent.getResources().getColor(COLORS[item.sectionPosition % COLORS.length]));
}
}
return convertView;
}
#Override public int getViewTypeCount() {
return 2;
}
#Override public int getItemViewType(int position) {
return getItem(position).type;
}
#Override
public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}
}
public class Item {
private String english;
private String hangul;
private String romanization;
public static final int ITEM = 0;
public static final int SECTION = 1;
public int sectionPosition;
public int listPosition;
public Item(String english, String hangul, String romanization) {
super();
this.english = english;
this.hangul = hangul;
this.romanization = romanization;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
public String getHangul() {
return hangul;
}
public void setHangul(String hangul) {
this.hangul = hangul;
}
public String getRomanization() {
return romanization;
}
public void setRomanization(String romanization) {
this.romanization = romanization;
}
public Item(int type, String text) {
this.type = type;
this.text = text;
}
#Override public String toString() {
return text;
}
}
public class FastScrollAdapter extends CustomAdapter implements SectionIndexer {
private Item[] sections;
public FastScrollAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
#Override protected void prepareSections(int sectionsNumber) {
sections = new Item[sectionsNumber];
}
#Override protected void onSectionAdded(Item section, int sectionPosition) {
sections[sectionPosition] = section;
}
#Override public Item[] getSections() {
return sections;
}
#Override public int getPositionForSection(int section) {
if (section >= sections.length) {
section = sections.length - 1;
}
return sections[section].listPosition;
}
#Override public int getSectionForPosition(int position) {
if (position >= getCount()) {
position = getCount() - 1;
}
return getItem(position).sectionPosition;
}
}
private boolean hasHeaderAndFooter;
private boolean isFastScroll;
private boolean addPadding;
private boolean isShadowVisible = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
isFastScroll = savedInstanceState.getBoolean("isFastScroll");
addPadding = savedInstanceState.getBoolean("addPadding");
isShadowVisible = savedInstanceState.getBoolean("isShadowVisible");
hasHeaderAndFooter = savedInstanceState.getBoolean("hasHeaderAndFooter");
}
initializeHeaderAndFooter();
initializeAdapter();
initializePadding();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isFastScroll", isFastScroll);
outState.putBoolean("addPadding", addPadding);
outState.putBoolean("isShadowVisible", isShadowVisible);
outState.putBoolean("hasHeaderAndFooter", hasHeaderAndFooter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Item item = (Item) getListView().getAdapter().getItem(position);
if (item != null) {
Toast.makeText(this, "Item " + position + ": " + item.text, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Item " + position, Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setChecked(isFastScroll);
menu.getItem(1).setChecked(addPadding);
menu.getItem(2).setChecked(isShadowVisible);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_fastscroll:
isFastScroll = !isFastScroll;
item.setChecked(isFastScroll);
initializeAdapter();
break;
case R.id.action_addpadding:
addPadding = !addPadding;
item.setChecked(addPadding);
initializePadding();
break;
case R.id.action_showShadow:
isShadowVisible = !isShadowVisible;
item.setChecked(isShadowVisible);
((PinnedSectionListView)getListView()).setShadowVisible(isShadowVisible);
break;
case R.id.action_showHeaderAndFooter:
hasHeaderAndFooter = !hasHeaderAndFooter;
item.setChecked(hasHeaderAndFooter);
initializeHeaderAndFooter();
break;
}
return true;
}
private void initializePadding() {
float density = getResources().getDisplayMetrics().density;
int padding = addPadding ? (int) (16 * density) : 0;
getListView().setPadding(padding, padding, padding, padding);
}
private void initializeHeaderAndFooter() {
setListAdapter(null);
if (hasHeaderAndFooter) {
ListView list = getListView();
LayoutInflater inflater = LayoutInflater.from(this);
TextView header1 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header1.setText("First header");
list.addHeaderView(header1);
TextView header2 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header2.setText("Second header");
list.addHeaderView(header2);
TextView footer = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
footer.setText("Single footer");
list.addFooterView(footer);
}
initializeAdapter();
}
#SuppressLint("NewApi")
private void initializeAdapter() {
getListView().setFastScrollEnabled(isFastScroll);
if (isFastScroll) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getListView().setFastScrollAlwaysVisible(true);
}
setListAdapter(new FastScrollAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
} else {
setListAdapter(new CustomAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
}
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Item: " + v.getTag() , Toast.LENGTH_SHORT).show();
}
}
}

Android - No enclosing instance of type is accessible [duplicate]

This question already has answers here:
"No enclosing instance of type" error while calling method from another class in Android
(4 answers)
Closed 8 years ago.
I am getting this error about "No enclosing instance of type accessible" when I try to create a new instance of my Item class. I am not sure what that means. So I don't know where to go from here. Please help. Thank you all in advance
public static class GreetFrag extends Fragment {
public static final String ARG_OBJECT = "object";
private List<RowItem> rowItems;
private List<Item> newList;
PinnedSectionListActivity pActivity = new PinnedSectionListActivity();
ListView lv;
ListView newLv;
Item item;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_greetfrag_swipeview, container, false);
//lv = (ListView) rootView.findViewById(R.id.myList);
newLv = (ListView) rootView.findViewById(R.id.newList);
//rowItems = new ArrayList<RowItem>();
newList = new ArrayList<MenuActivity.GreetFrag.PinnedSectionListActivity.Item>();
String[] english = {"Hi (to same age or younger)",
"Yo!", "Hey!", "Good to see ya.","Hello?","Who dis?","What’s up?","How ya been?","Yeah, I’m good!","Good!","So-so.","Sucky.",
"Whatcha been up to?","Long time no see!","It’s been a while.","You’re still alive?","You need to lose some weight!",
"Your face has gotten chubbier!","Your mom has gotten chubbier!","Goodbye","Goodbye (general use)","Goodbye (to a person staying)",
"Goodbye (to a person leaving)","Goodbye (when both parties are parting)","Bye bye","See ya!","Later!","I’m out.","Be careful!",
"Good morning(formal)", "G'morning!", "Sleep well?", "It's a beautiful evening!", "Good evening!(formal)", "Evenin", "Sweet dreams!",
"Dream about me!", "Sorry!(informal)", "FOrgive me!", "Oops!", "My bad!", "No worries", "It's all good", "No problem", "Forget aboutit",
"Can you please do me a favor?", "Please, accept my heart!", "Excuse me", "Move our of my way!", "Be careful!", "My name is Michael",
"I am from America", "I have yello fever", "I am 18 years ola dn full of testosterone", "I like hairy girls",
"Can you setme up on a blind date with one of your hottie frineds?", "Nice to meet you", "What's your name?",
"Have we met before?", "Do you have the time?", "How old are you?"};
String[] roman = {"Annyeong",
"Ya!", "Imma!", "Banga banga","Yeoboseyo?","Nuguseyo?","Wannya?","Jal jinaeni?","Eung, jal jinae!","Joa!","Geunyang geurae.","Guryeo.",
"Mohago sarannya?","Oretmaniyeyo!","Olman!","Saraisseonnya?","Sal jom bbaeyagetda!",
"Eolguli tongtonghae!", "Neone eomma saljom jjisyeosseo!","Jal jinae~","Annyeong~","Jal isseo~","Tto boja!",
"Jal ga~","Jal jinae~","Bai bai","Najungebwa!","Na ganda.","Josimhae!", "Annyeonghi jumusyeoseoyo","Joeun achim!",
"Jal Jasseo?","Joeun bam!", "Anneonghi jumuseyo", "jalja", "Joeun kkumkkwo!", "Nae kkumkkwo!", "Mian!",
"Yongseohaejwo!", "At!", "Silsu!", "Geokjeonghajima", "Da gwaenchana", "Munjae eopsseo", "Ijeobeoryeo",
"Budi butak jom deureojullae?", "Jebal je mamaeul badajuseyo!", "Sillyehapnida/Jamsimanyo", "Bikyeo!",
"Josim!", "Je ireumeun Michael ipsida", "Jeoneun migukeseo wasseupnida", "Jeoneun dongyanginman joahapnida",
"Jeoneun yeolyeodeolssaligo himi neomchipnida", "Jeoneun teol maneun yeojaga jossepnida",
"Jal sang-gin chingurang na sogaeting sikyejo?", "Mannaseo ban-gapseupnida", "Ireumi mwoyeyo?",
"Uri mannanjeok itnayo?", "Jgeum myeossiyeyo?", "Naiga eotteoke dwae?"};
String[] hangul = {"안녕",
"야!", "임마!", "방가 방가","여보세요?","누구세요?","왔냐?","잘 지내니?","응, 잘 지내!","좋아!","그냥 그래.","구려.",
"뭐하고 살았냐?","오랫만이예요!","올만!","살아있었냐?","살 좀 빼야겠다!",
"얼굴이 통통해!","너네 엄마 살좀 찌셨어!","잘 지내~","안녕~","잘 있어~",
"잘 가~","잘 지내~","바이 바이","또 보자!","나중에봐!","나 간다.","조심해!",
"안녕히 주무셨어요", "좋은 아침!", "잘 잤어?", "좋은 밤!", "안녕히 주무세요",
"잘자", "좋은 꿈꿔!", "내 꿈꿔!", "미안!", "용서해줘!", "앗!", "실수!", "걱정하지마",
"다 괜찮아","문제없어", "잊어버려", "부디 부탁 좀 들어줄래?", "제발 제 맘을 받아주세요!",
"실례합니다/잠시만요", "비켜!", "조심!", "제 이름은 마이클입니다", "저는 미국에서 왔습니다",
"저는 동양인만 좋아합니다", "저는 열여덟살이고 힘이 넘칩니다", "저는 털 많은 여자가 좋습니다",
"잘 생긴 친구랑 나 소개팅 시켜줘?", "만나서 반갑습니다", "이름이 뭐예요?", "우리 만난적 있나요?",
"지금 몇시예요?", "나이가 어떻게 돼?"};
//Populate the List
for (int i = 0; i < english.length; i++) {
item = new Item(english[i], hangul[i], roman[i], 1, "test");
newList.add(item);
}
// Set the adapter on the ListView
LazyAdapter adapter = new LazyAdapter(getActivity(), R.layout.view_lessons, rowItems);
//lv.setAdapter(adapter);
/**
lv.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
**/
return rootView;
}
public class PinnedSectionListActivity extends ListActivity implements OnClickListener {
public class CustomAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter {
Context context;
List <Item> items;
private final int[] COLORS = new int[] {
R.color.green_light,
R.color.orange_light,
R.color.blue_light,
R.color.red_light };
class ViewHolder {
int sectionPosition;
int listPosition;
TextView english;
TextView romanization;
TextView hangul;
RelativeLayout card;
}
public CustomAdapter(Context context, int resourceId, List<Item> items) {
super(context, resourceId, items);
this.context = context;
}
public CustomAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
final int sectionsNumber = 'Z' - 'A' + 1;
prepareSections(sectionsNumber);
int sectionPosition = 0, listPosition = 0;
for (int i=0; i< sectionsNumber; i++) {
String title = null;
final String []country = {
"Korean", "Japanese", "Chinese", "Cambodian", "Loas", "Taiwamese"
};
final String [] CATEGORY = {
"Language",
"sports",
"love",
"luxury",
"vacation",
"games",
"home",
"travel",
"electronics",
"movies",
};
switch (('A' + i)) {
case ('A' + 0):
title = country[0];
break;
case ('A' + 1):
title = country[1];
break;
case ('A' + 2):
title = country[2];
break;
case ('A' + 3):
title = country[3];
break;
case ('A' + 4):
title = country[4];
break;
case ('A' + 5):
title = country[5];
break;
default:
break;
}
//Create a new Item class with section header and Name
Item section = new Item(Item.SECTION, title + " " + i);
//Item section = new Item(Item.SECTION, String.valueOf((char)('A' + i)));
section.sectionPosition = sectionPosition;
section.listPosition = listPosition++;
onSectionAdded(section, sectionPosition);
add(section);
final int itemsNumber = CATEGORY.length;
//(int) Math.abs((Math.cos(2f*Math.PI/3f * sectionsNumber / (i+1f)) * 25f));
// For loop to iterate the exact number of itemNumber
for (int j = 0;j < CATEGORY.length;j++) {
//Item item = new Item(Item.ITEM, section.text.toUpperCase(Locale.KOREA) + " - " + j);
Item item = new Item(Item.ITEM, CATEGORY[j]);
item.sectionPosition = sectionPosition;
item.listPosition = listPosition++;
add(item);
}
sectionPosition++;
}
}
protected void prepareSections(int sectionsNumber) { }
protected void onSectionAdded(Item section, int sectionPosition) { }
#Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view = null;
Item item = getItem(position);
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.view_lessons, null);
view.card = (RelativeLayout) convertView.findViewById(R.id.card);
view.romanization = (TextView)convertView.findViewById(R.id.romanization);
view.english = (TextView)convertView.findViewById(R.id.english_translation);
view.hangul = (TextView)convertView.findViewById(R.id.hangul);
convertView.setTag(view);
} else {
view = (ViewHolder)convertView.getTag();
view.romanization.setText(item.getRomanization());
view.english.setText(item.getEnglish());
view.hangul.setText(item.getHangul());
Animation animation = AnimationUtils.loadAnimation(context, R.anim.card_animation);
view.card.startAnimation(animation);
if (item.type == Item.SECTION) {
//view.setOnClickListener(PinnedSectionListActivity.this);
view.english.setBackgroundColor(parent.getResources().getColor(COLORS[item.sectionPosition % COLORS.length]));
}
}
return convertView;
}
#Override public int getViewTypeCount() {
return 2;
}
#Override public int getItemViewType(int position) {
return getItem(position).type;
}
#Override
public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}
}
class Item {
private String english;
private String hangul;
private String romanization;
public static final int ITEM = 0;
public static final int SECTION = 1;
public final int type;
public final String text;
public int sectionPosition;
public int listPosition;
public Item(String english, String hangul, String romanization, int type, String text) {
super();
this.type = type;
this.text = text;
this.english = english;
this.hangul = hangul;
this.romanization = romanization;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
public String getHangul() {
return hangul;
}
public void setHangul(String hangul) {
this.hangul = hangul;
}
public String getRomanization() {
return romanization;
}
public void setRomanization(String romanization) {
this.romanization = romanization;
}
public Item(int type, String text) {
this.type = type;
this.text = text;
}
#Override public String toString() {
return text;
}
}
public class FastScrollAdapter extends CustomAdapter implements SectionIndexer {
private Item[] sections;
public FastScrollAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
#Override protected void prepareSections(int sectionsNumber) {
sections = new Item[sectionsNumber];
}
#Override protected void onSectionAdded(Item section, int sectionPosition) {
sections[sectionPosition] = section;
}
#Override public Item[] getSections() {
return sections;
}
#Override public int getPositionForSection(int section) {
if (section >= sections.length) {
section = sections.length - 1;
}
return sections[section].listPosition;
}
#Override public int getSectionForPosition(int position) {
if (position >= getCount()) {
position = getCount() - 1;
}
return getItem(position).sectionPosition;
}
}
private boolean hasHeaderAndFooter;
private boolean isFastScroll;
private boolean addPadding;
private boolean isShadowVisible = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
isFastScroll = savedInstanceState.getBoolean("isFastScroll");
addPadding = savedInstanceState.getBoolean("addPadding");
isShadowVisible = savedInstanceState.getBoolean("isShadowVisible");
hasHeaderAndFooter = savedInstanceState.getBoolean("hasHeaderAndFooter");
}
initializeHeaderAndFooter();
initializeAdapter();
initializePadding();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isFastScroll", isFastScroll);
outState.putBoolean("addPadding", addPadding);
outState.putBoolean("isShadowVisible", isShadowVisible);
outState.putBoolean("hasHeaderAndFooter", hasHeaderAndFooter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setChecked(isFastScroll);
menu.getItem(1).setChecked(addPadding);
menu.getItem(2).setChecked(isShadowVisible);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_fastscroll:
isFastScroll = !isFastScroll;
item.setChecked(isFastScroll);
initializeAdapter();
break;
case R.id.action_addpadding:
addPadding = !addPadding;
item.setChecked(addPadding);
initializePadding();
break;
case R.id.action_showShadow:
isShadowVisible = !isShadowVisible;
item.setChecked(isShadowVisible);
((PinnedSectionListView)getListView()).setShadowVisible(isShadowVisible);
break;
case R.id.action_showHeaderAndFooter:
hasHeaderAndFooter = !hasHeaderAndFooter;
item.setChecked(hasHeaderAndFooter);
initializeHeaderAndFooter();
break;
}
return true;
}
private void initializePadding() {
float density = getResources().getDisplayMetrics().density;
int padding = addPadding ? (int) (16 * density) : 0;
getListView().setPadding(padding, padding, padding, padding);
}
private void initializeHeaderAndFooter() {
setListAdapter(null);
if (hasHeaderAndFooter) {
ListView list = getListView();
LayoutInflater inflater = LayoutInflater.from(this);
TextView header1 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header1.setText("First header");
list.addHeaderView(header1);
TextView header2 = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
header2.setText("Second header");
list.addHeaderView(header2);
TextView footer = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, list, false);
footer.setText("Single footer");
list.addFooterView(footer);
}
initializeAdapter();
}
#SuppressLint("NewApi")
private void initializeAdapter() {
getListView().setFastScrollEnabled(isFastScroll);
if (isFastScroll) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getListView().setFastScrollAlwaysVisible(true);
}
setListAdapter(new FastScrollAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
} else {
setListAdapter(new CustomAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
}
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Item: " + v.getTag() , Toast.LENGTH_SHORT).show();
}
}
}
Here is the whole fragment.
If Both classes Item and CustomAdapter reside in same java file then you need to remove public keyword from Item class.
Java only permits one public class on a .java file since Java designers chose a strict approach that enforces their idea of good design practices, and this is part of that theme.
For more information, check following link :
http://www.artima.com/weblogs/viewpost.jsp?thread=7555
It looks like your:
public class Item
is outside your CustomAdapter class, move it inside CustomAdapter class, or make Item non public class.
Also, if you move it inside your CustomAdapter then consider making it static class. From your code you are not using enclosing class (here CustomAdapter) inside Item, so there is no need to make Item non static.

Categories

Resources