startActivity时出错

时间:2017-01-01 04:18:02

标签: java android onitemclicklistener

祝大家新年快乐。 我遇到onItemClickListener方法的问题,因为当我尝试向位置和id干杯时,在我的第二个活动中,我得到了零。 这是代码,我依赖于数组中图像的位置,这就是我需要准确获取位置/ id的原因。 + 我在两个活动中重复我的数组,因为我不知道如何从第二个活动中访问它?

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.

    }
}

2 个答案:

答案 0 :(得分:0)

您必须按照以下方式获取值,

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();

答案 1 :(得分:0)

你误解了你应该使用意图的方式。 正确的形式是这样的:

getIntExtra

使用{{1}}时,第二个参数是默认值。如果您的意图中没有该标记的值,它将返回该默认值。 有关详细信息,请参阅Google docs