I was using every piece of code I could find for my problem but seems that my understaning of android is still not good enough and I cannot make this thing work. I have an app with tabs. Each tab contains different list view. One of the tabs contains "favouirte list view". Users can add item from other tabs to favourite list view. I have created sqlite db and on button press item is inserted into the db and from the db list view is populated.
I have tried notifyDataSetChanged();, favouriteList.invalidateViews();, favouriteList.refreshDrawableState(); but none of these worked for me. I guess I do not understand how thing works and I am making some mistakes.
Here is my code:
Main Activity:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private int counter = 0;
private ViewPager viewPager;
private ActionBar actionBar;
private String[] tabs = { "Favourites", "Films", "Other", "All", "NSFW" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
TabsPagerAdapter mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
actionBar.setSelectedNavigationItem(1);
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Favourites Fragment:
public class FavouritesFragment extends Fragment {
DBAdapter myDb;
ListView favouritesList;
SimpleCursorAdapter myCursorAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_ulubione, container, false);
favouritesList= (ListView)v.findViewById(R.id.favouritesList);
registerForContextMenu(ulubioneList);
openDB();
myDb.populateDataBaseOnStart();
populateListViewFromDB();
registerClickCallback();
return v;
}
public void openDB() {
myDb = new DBAdapter(getActivity());
myDb.open();
}
public void onDestroy() {
super.onDestroy();
closeDB();
}
private void closeDB() {
myDb.close();
}
private void populateListViewFromDB() {
Cursor cursor = myDb.getFavourites();
getActivity().startManagingCursor(cursor);
String[] fromFieldNames = new String[]{DBAdapter.KEY_TITLE};
int[] toViewIDs = new int[]{R.id.item_name};
myCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.item_layout, cursor, fromFieldNames, toViewIDs);
ulubioneList.setAdapter(myCursorAdapter);
}
public void registerClickCallback() {
ulubioneList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long idDB) {
String title = "";
Cursor cursor = myDb.getRow(idDB);
if (cursor.moveToFirst()) {
title = cursor.getString(DBAdapter.COL_FILENAME);
}
cursor.close();
int RawId = getResources().getIdentifier("raw/" + title, "raw", getActivity().getPackageName());
if (MyMediaPlayer.mp == null) MyMediaPlayer.mp = new MediaPlayer();
else if (MyMediaPlayer.mp.isPlaying()) MyMediaPlayer.mp.stop();
MyMediaPlayer.mp.reset();
String fileName = "android.resource://" + getActivity().getPackageName() + "/" + RawId;
try {
MyMediaPlayer.mp.setDataSource(getActivity().getApplicationContext(), Uri.parse(fileName));
MyMediaPlayer.mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
MyMediaPlayer.mp.start();
}
});
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Set As");
menu.add(0, v.getId(), 0, "Ringtone Sound");
menu.add(0, v.getId(), 0, "SMS Sound");
menu.add(0, v.getId(), 0, "Add to favourites");
menu.add(0, v.getId(), 0, "Remove from favourites");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
String title = "";
String fileName = "";
Cursor cursor = myDb.getRow(index);
if (cursor.moveToFirst()) {
fileName = cursor.getString(DBAdapter.COL_FILENAME);
title = cursor.getString(DBAdapter.COL_TITLE);
}
cursor.close();
if (item.getTitle() == "Ringtone Sound") {
setAsRingtone(title, fileName);
Toast.makeText(getActivity().getApplicationContext(), title.toUpperCase() + " set as Ringtone Sound", Toast.LENGTH_LONG).show();
} else if (item.getTitle() == "SMS Sound") {
setAsSMS(title, fileName);
Toast.makeText(getActivity().getApplicationContext(), "Ustawiono " + title.toUpperCase() + " Jako Dźwięk SMS", Toast.LENGTH_LONG).show();
} else if (item.getTitle() == "Add to favourites") {
myDb.addToFavourites(title);
//myCursorAdapter.notifyDataSetChanged();
populateListViewfromDB();
Toast.makeText(getActivity().getApplicationContext(), "Dodano " + title.toUpperCase() + " Do Ulubionych", Toast.LENGTH_LONG).show();
} else if (item.getTitle() == "Remove from favourites") {
myDb.removeFromFavourites(title);
favouritesList.refreshDrawableState();
populateListViewFromDB()
Toast.makeText(getActivity().getApplicationContext(), "Usunięto " + title.toUpperCase() + " z Ulubionych", Toast.LENGTH_LONG).show();
} else {
return false;
}
return true;
}
Database is updated correctly.
Aucun commentaire:
Enregistrer un commentaire