【问题标题】:error when startActivity启动活动时出错
【发布时间】:2017-01-01 04:18:01
【问题描述】:

祝大家新年快乐。 我在使用 onItemClickListener 方法时遇到问题,因为当我尝试为 position 和 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.

    }
}

【问题讨论】:

  • 新年快乐!!你的错误在哪里!
  • 你试过让你的班级实现onItemClickListener吗?如:公共类 MainActivity 扩展 AppCompatActivity 实现 onItemClickListener
  • Charuka,错误在于获取发送到第二个活动的正确位置和 id,正是这里:int id=0; getIntent().getIntExtra("id",id);但问题解决了。谢谢。
  • 接受一个正确的答案@Lujain

标签: java android onitemclicklistener


【解决方案1】:

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

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

当你使用 getIntExtra 时,第二个参数是默认值。如果您的意图中该标签没有这样的值,它将返回该默认值。 请参阅Google docs 了解更多信息

【讨论】:

  • 谢谢 smartiz,你说得对,我误会了。我认为第一个参数是第一个活动在 putExtra("", ) 中给出的字符串名称。另外,您又是对的,因为我是初学者,并被要求开发购物应用程序。我不知道我将如何成功!
  • 祝你在当前任务中取得成功@Lujain:D
【解决方案2】:

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

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

【讨论】:

  • 我试过这个:Bundle received = getIntent().getExtras(); int b = received.getInt("位置");它奏效了。
  • @Lujain,如果您发现我的答案已使用完整,请将此答案标记为接受,以便其他人认为此答案有用。
猜你喜欢
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多