mardi 2 juin 2015

Context menu shows correct data but listview doesn't

I have a really strange problem that I've been trying to solve the last hours but I can't find whats wrong. I have a SQLite database containing names and a small amount of text. A listview who gets populated with the names from it, when i click on the listview I see the text that is connected to the names. But I also have a "Add to favorite" and when i add something to my favorite list it displays on another activity. The displayed info is correct in the new/favorite listview (Names, title and a image) but when i click on it its wrong data that is displayed.

I have 10 rows in my original listview and in my "favorite" listview is it the same order as in the first one. If I add e.g a movie..Casablanca that is on position 7 in my original list,it will place it self on the first position in my new listview (position 0) and it shows correct actors, covers and author. BUT when i click it to see more info(starts new activity). It only shows the info from the the original listview with the info from the movie on position 0.

But if i click on my context menu to check it. It displays all the correct data I want to see. It so strange and I hope there is someone that know what the problem can be? I've tried a lot of different combos and solutions with arrays and strings/stringbuilder and so on. This is the last code i wrote before i came here.

 public class Favorites extends ActionBarActivity {

DatabaseHelper dbHelper;
ListView favoritesListView;
Cursor myCursor;
String[] myStringArray = new String[3];
ListViewCursorAdapter myFavsAdapter;
String openBookCmeny = "Öppna boken";
String rmvFavsCmeny = "Ta bort från dina favoriter";
String webSiteCmeny = "Klicka här för mer information på bokens hemsida";
String showWebSiteCmeny;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favorites);

    dbHelper = DatabaseHelper.getInstance(this);
    myCursor = dbHelper.visaFavoriter();
    dbHelper.getWritableDatabase();
    myFavsAdapter = new ListViewCursorAdapter(this, myCursor);
    favoritesListView = (ListView)findViewById(R.id.favoritesListView);
    favoritesListView.setAdapter(myFavsAdapter);
    registerForContextMenu(favoritesListView);


    favoritesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Cursor c =  (Cursor)myFavsAdapter.getItem(view.getId());
      myStringArray[1]= c.getString(4);

      Intent i = new Intent(Favorites.this, VisaBoken.class);
      i.putExtra(null, myStringArray);
      startActivity(i);

}
    });

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterView.AdapterContextMenuInfo info =  (AdapterView.AdapterContextMenuInfo) menuInfo;

    Cursor c = ((Cursor) myFavsAdapter.getItem(info.position));
    String cTitle = c.getString(2);

    menu.setHeaderTitle(cTitle);
    menu.add(0, v.getId(), 0, openBookCmeny);
    menu.add(0, v.getId(), 0, rmvFavsCmeny);
    menu.add(0, v.getId(), 0, webSiteCmeny);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

    Cursor c = ((Cursor) myFavsAdapter.getItem(info.position));
    myStringArray[0] = c.getString(0);
    myStringArray[1] = c.getString(4);
    showWebSiteCmeny = c.getString(6);

    if (item.getTitle() == openBookCmeny){
        Intent i = new Intent(Favorites.this, VisaBoken.class);
        i.putExtra(null, myStringArray);
        startActivity(i);
    }

    else if (item.getTitle() == rmvFavsCmeny){
        dbHelper.removeOneFavorite(myStringArray[0]);
        recreate();

    }
    else if (item.getTitle() == webSiteCmeny){
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(showWebSiteCmeny));
        startActivity(browserIntent);
    }
    return super.onContextItemSelected(item);
}

The class that gets the intents and info to display it.

 public class VisaBoken extends ActionBarActivity implements AdapterView.OnItemClickListener {

ActionBarDrawerToggle myDrawerToggle;
DrawerLayout myDrawerLayout;
ListView myDrawerListView;
ArrayList<DrawerMenuName> myDrawerMenuName = new ArrayList<>();
DatabaseHelper dbHelper;
public static final String COL_ID = "_id";
public static String Tag = "checkIDfromDB";
TextView visaBokenTV;
String[] myStringArray = new String[2];
String[] myStringArray2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_visa_boken);
    visaBokenTV = (TextView)findViewById(R.id.visaBokenTV);
    Bundle extras = getIntent().getExtras();

    myStringArray = extras.getStringArray(null);
    visaBokenTV.setText(myStringArray[1]);

    myDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    dbHelper = DatabaseHelper.getInstance(this);
    dbHelper.getWritableDatabase();

Aucun commentaire:

Enregistrer un commentaire