简体   繁体   中英

error when startActivity

happy new year to all of you. I am having a problem with the method onItemClickListener, because when I tried to toast the position and id ,in my second activity, I get zeros. here is the code, I rely on the position of the image in the array that is why I need to get the position/id accurately. + I am duplicating my array in both activities because I don't know how to access it from the second activity?

MainActivity.java

    package swe.trial;
import android.content.Context;
import android.widget.ImageView;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.AdapterView;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        GridView items = (GridView) findViewById(R.id.itemsList);
        items.setAdapter(new item_adapter(this));
        items.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //if an item is clicked or selceted,then go to the activity itemDetails:
                Intent i= new Intent (MainActivity.this, itemDetails.class);
                i.putExtra("position", position);
                i.putExtra("id", id);
                startActivity(i);
            }
        });

    }

}


class item_adapter extends BaseAdapter {
    Integer[] picsId = {
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5,
            R.drawable.pic6,
            R.drawable.pic7};
    private Context context;


    public item_adapter(Context context) {
        this.context = context;
    }


    public Object getItem(int position) {
        return null;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imgview= new ImageView(context);
        imgview.setLayoutParams(new GridView.LayoutParams(250,250));
        imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imgview.setPadding(20,20,20,20);

        imgview.setImageResource(picsId[position]);
        return imgview;
    }


    public long getItemId(int position) {
        return position;
    }
    public int getCount (){
        return picsId.length;
    }
}

itemDetails.java

    package swe.trial;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.net.Uri;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by good on 1/01/17.
 */

public class itemDetails extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item);
        ArrayList<item> items = new ArrayList<item>();
//        items item1=
//        Bundle d = getIntent().getExtras();
        int id=0;getIntent().getIntExtra("id",id);
        int position=0;
        getIntent().getIntExtra("position", position);
        Toast.makeText( this, "id= " +id + " .  and posistion=  "+ position, Toast.LENGTH_LONG).show();

        Integer[] picsId = {
                R.drawable.pic1,
                R.drawable.pic2,
                R.drawable.pic3,
                R.drawable.pic4,
                R.drawable.pic5,
                R.drawable.pic6,
                R.drawable.pic7
        };

        ImageView imgview = (ImageView) findViewById(R.id.item_image);
        imgview.setImageResource(picsId[id+ 1]);
        TextView txt= (TextView) findViewById(R.id.pricetxtview);
        txt.setText("This is the description provided." + id);
        //(id);


//        item item1 = {"Red rose", "@/drawable/", 1, 1.25};
//        items.add(item1);
        // now i will search for the array that holds the given id. and i will retrieve its info and display it again
        // in the new layout.

    }
}

You have to fetch value according the following way,

int id=0;
 int position=0;
 i=getIntent().getIntExtra("id",id);
 position= getIntent().getIntExtra("position", position);
 Toast.makeText( this, "id= " +id + " .  and posistion=  "+ position, Toast.LENGTH_LONG).show();

You misunderstand the way you should use intents. The right form is like this :

 int id =getIntent().getIntExtra("id",0);
 int position = getIntent().getIntExtra("position", 0);

when you use getIntExtra , the second parameter is the default value. In case there is not such value with that tag in your intent, it will return that default value. See Google docs for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM