vendredi 1 avril 2016

Java/Android - List Adapter/Cursor Difficulties

I am working on part of an app that should redirect the user to an activity which displays in image when they click on an item in a List View. Two of the items works - the most recent one (position 0) does not. The screen merely (very briefly) flashes black and then goes back to the original page.

Activity with List Adapter

    public class lunchHome extends AppCompatActivity {
private SimpleCursorAdapter adapter;
private TextView headerText;
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.configure_icon:

                Intent i = new Intent(this, preferences.class);
                startActivity(i);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      int discriminator = 0;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lunch_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
headerText = (TextView)findViewById(R.id.headerText);
        databaseHelper dbHelper = new databaseHelper(this);
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
        if(preferences.getInt("Location", 2) == 3) {
            discriminator = 3;
        } else if(preferences.getInt("Location", 2) == 2){
            discriminator = 2;
        }

        if(discriminator == 2) {
            headerText.setText("Main Boards");
        } else if (discriminator == 3) {
            headerText.setText("Speciality");
        }

        Button ltoButton = (Button)findViewById(R.id.lunchLTOButton);
        Button standardButton = (Button)findViewById(R.id.lunchMainButton);

        standardButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(lunchHome.this, lunchStandard.class);
                startActivity(intent);

            }
        });

        ltoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(lunchHome.this, lunchLTOs.class);
                startActivity(intent);
            }
        });






Cursor results = db.rawQuery("SELECT _id, name, icon, buildImage FROM " + databaseHelper.TABLE_NAME + " WHERE location = " + discriminator + " and LTO = " + 1 + " ORDER BY " + databaseHelper.ITEM_ID + " DESC LIMIT 3", null);

String[] from = new String [] {databaseHelper.ITEM_NAME};
int[] to = new int[] {R.id.newName};

        adapter = new SimpleCursorAdapter(this, R.layout.item_list, results, from, to, 0);
        final ListView listContainer = (ListView) findViewById(R.id.listContainer);
        listContainer.setAdapter(adapter);

        listContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor cursor = (Cursor) listContainer.getItemAtPosition(position);
                // int imageId = cursor.getInt(cursor.getColumnIndex("buildImage"));
                byte[] imageId = cursor.getBlob(cursor.getColumnIndex("buildImage"));
Intent intent = new Intent(lunchHome.this, midamcorp.com.burgerkingapp.buildImage.class);
                intent.putExtra("imageID", imageId );
                startActivity(intent);

            }
        });

Activity it should be redirecting to:

public class buildImage extends AppCompatActivity {
    private SimpleCursorAdapter adapter;
private Button back;
    ImageView buildContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_build_image);
        super.onCreate(savedInstanceState);
         buildContainer = (ImageView) findViewById(R.id.fullSizeImage);
        back = (Button) findViewById(R.id.backButton);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void onStart() {
        super.onStart();
        Intent i = getIntent();
        if (i.hasExtra("position"))

        {
            int position = i.getExtras().getInt("position");
            int lto = i.getExtras().getInt("lto");
            int location = i.getExtras().getInt("location");
            gridAdapter adapter = new gridAdapter(this, lto, location);

            // buildContainer.setImageResource(adapter.buildImages[position]);


            Bitmap buildImage = BitmapFactory.decodeByteArray(adapter.buildImages.get(position), 0, adapter.buildImages.get(position).length);
            buildContainer.setImageBitmap(buildImage);

        } else

        {
            // int imageId = i.getIntExtra("imageID", 0);

            byte[] imageId = i.getByteArrayExtra("imageID");
            Bitmap buildImage = BitmapFactory.decodeByteArray(imageId, 0, imageId.length);
            buildContainer.setImageBitmap(buildImage);

            // buildContainer.setImageResource(imageId);
        }
    }
    }

Some debugging output

this = {lunchHome$3@4271} 
listContainer = {ListView@4273} "android.widget.ListView{42c8483 VFED.VC.. .F.P..I. 0,306-768,506 #7f0d0080 app:id/listContainer}"
parent = {ListView@4273} "android.widget.ListView{42c8483 VFED.VC.. .F.P..I. 0,306-768,506 #7f0d0080 app:id/listContainer}"
view = {LinearLayout@4274} "android.widget.LinearLayout{7a68600 V.E...... ......I. 0,0-495,69}"
position = 0
id = 36
cursor = {SQLiteCursor@4275} 
imageId = {byte[808250]@4276} 
intent = {Intent@4277} "Intent { cmp=midamcorp.com.burgerkingapp/.buildImage }"

I have been messing with this for longer than I care to admit. Thank you so much for any help!

EDIT: It is as if it is not even starting the new activity for some reason.

Aucun commentaire:

Enregistrer un commentaire