0

在这里,我需要将图像传递到画廊。
在一个类中,我有 10 个按钮,在这里,当单击 1 个按钮时,我需要将 1 个按钮相关图像传递到图库中,当单击 2 个按钮时,我需要将 2 个按钮相关图像传递到图库中。
我尝试使用下面的代码,但是当单击 1 按钮时,它工作正常意味着图像显示在画廊中,但是当我单击 2,3 ...按钮时,我得到了黑屏。

请任何人建议我如何将不同的数组传递到相同的活动中。

     HomeDemo.class:
   public class HomeDemo extends Activity implements OnTouchListener{
Button home1,home2,home3,home4;
ImageButton back;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.homedemo);
        home1=(Button)findViewById(R.id.button1);
        home2=(Button)findViewById(R.id.button2);
        home3=(Button)findViewById(R.id.button3);
        home4=(Button)findViewById(R.id.button4);
        home1.setOnTouchListener(this);
        home2.setOnTouchListener(this);
        home3.setOnTouchListener(this);
        home4.setOnTouchListener(this);
 }
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    if(v==home1)
    {
        Intent i=new Intent(HomeDemo.this,Home1.class);
        i.putExtra("k1", 1);
        startActivity(i);
    }
    if(v==home2)
    {
        Intent i=new Intent(HomeDemo.this,Home1.class);
        i.putExtra("k2", 2);
        startActivity(i);
    }
    if(v==home3)
    {
        Intent i=new Intent(HomeDemo.this,Home1.class);
        i.putExtra("k3", 3);
        startActivity(i);
    }
    if(v==home4)
    {
        Intent i=new Intent(HomeDemo.this,Home1.class);
        i.putExtra("k4", 4);
        startActivity(i);
    }

    return false;
}
}

       Home1 .class
       public class Home1 extends Activity implements
          AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory{
       private TextSwitcher mSwitcher;
   ImageButton home,sound;
Gallery g;
static int nextbtn = 0;
static int prebtn = 0;
ImageAdapter iadapter;
int galpos;
int countk1=0;
int countk2=0;
int countk3=0;
int countk4=0;

public int ht;
public int wt;

public static int motiongal = 3000; 
RelativeLayout l1,l2;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.home1);

    home=(ImageButton)findViewById(R.id.homeimage);
    home.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Intent i=new Intent(Home1.this,HomeDemo.class);
            startActivity(i);
            return false;
        }
    });
     sound=(ImageButton)findViewById(R.id.soundimage);
      sound.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return false;
        }
    });
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    ht = displaymetrics.heightPixels;
    wt = displaymetrics.widthPixels;
 l1=(RelativeLayout)findViewById(R.id.linear);
 l2=(RelativeLayout)findViewById(R.id.linear1);
 l1.setVisibility(View.INVISIBLE);
 l2.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        l1.setVisibility(View.VISIBLE);
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
          public void run() {
            //Do something after 100ms
              l1.setVisibility(View.INVISIBLE);
          }
        }, 1500);
        return false;
    }
});


    mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
    mSwitcher.setFactory(this);
    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in));
    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));
    mSwitcher.startLayoutAnimation();
    try
    {
    g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));
    g.setHorizontalScrollBarEnabled(false);
    Bundle bundle = getIntent().getExtras();
    galpos = bundle.getInt("GALPOS");
     countk1 = getIntent().getIntExtra("k1", 0); 
     countk2 = getIntent().getIntExtra("k2", 0); 
     countk3 = getIntent().getIntExtra("k3", 0); 
     countk4 = getIntent().getIntExtra("k4", 0); 

    g.setSelection(galpos);        
    g.setAnimationDuration(motiongal);


    } catch(Exception e){

  }
    g.setOnItemSelectedListener(this);


    ImageButton nextButton = (ImageButton) findViewById(R.id.nextimage);
    nextButton.setOnClickListener(nextButtonOnClick);

    ImageButton preButton = (ImageButton) findViewById(R.id.preimage);
    preButton.setOnClickListener(preButtonOnClick);



    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            //Toast.makeText(GalleryActivity.this, "" + mTextNames[position] , Toast.LENGTH_SHORT).show();
             if(position==12)
            {
                Intent i=new Intent(Home1.this, HomeDemo.class);
                startActivity(i);
            }
            l1.setVisibility(View.VISIBLE);
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
              public void run() {
                //Do something after 100ms
                  l1.setVisibility(View.INVISIBLE);
              }
            }, 1500);
        }
    });

}



public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    mSwitcher.setText(mTextNames[position]);
    nextbtn = position;



}

public OnClickListener nextButtonOnClick = new OnClickListener(){
    public void onClick(View v) {           

        if(nextbtn < (mTextNames.length-1))
        {     
            try
            {               
            mSwitcher.setText(mTextNames[nextbtn+1]);
            mSwitcher.startLayoutAnimation();               
            mSwitcher.getDrawingTime();
            g.setSelection(nextbtn+1);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);  

            }
            catch(Exception e)
            {}

        }else
        {
            try
            {               
            mSwitcher.setText(mTextNames[0]);   
            mSwitcher.startLayoutAnimation();               
            mSwitcher.getDrawingTime();
            g.setSelection(0);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);                  
            }
            catch(Exception e)
            {}
        }

      } 
};   

public OnClickListener preButtonOnClick = new OnClickListener(){
    public void onClick(View v) {           

        if(nextbtn > 0)
        {
            try
            {               

            mSwitcher.startLayoutAnimation();               
            mSwitcher.getDrawingTime();
            g.setSelection(nextbtn-1);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);

            }
            catch(Exception e)
            {}


        }else
        {
            try
            {               
            mSwitcher.setText(mTextNames[mTextNames.length-1]);
            mSwitcher.startLayoutAnimation();               
            mSwitcher.getDrawingTime();
            g.setSelection(mTextNames.length-1);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);                  
            }
            catch(Exception e)
            {}

        }

      } 
};    


public void onNothingSelected(AdapterView<?> parent) {
}

public View makeView() {
    TextView i = new TextView(this);
    i.setTextSize(18);

    i.setGravity(1);
    return i;
}

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getView() {
        // TODO Auto-generated method stub
        return 0;
    }

    public int getCount() {
        return mPhotos.size();
    }

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

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
       if(countk1==1)
       {
        i.setImageResource(mPhotos.get(position));
       }
       if(countk2==1)
       {
        i.setImageResource(mPhotos1.get(position));
       }
       if(countk2==3)
       {
        i.setImageResource(mPhotos1.get(position));
       }
       if(countk2==4)
       {
        i.setImageResource(mPhotos1.get(position));
       }

        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setLayoutParams(new Gallery.LayoutParams(
               LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        return i;
    }

    private Context mContext;

}


private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
      return e2.getX() > e1.getX();
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
      int kEvent;
      if(isScrollingLeft(e1, e2)){ //Check if scrolling left
        kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
      }
      else{ //Otherwise scrolling right
        kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
      }
      onKeyDown(kEvent, null);
      return true;  
    }


private Integer[] mImageIds = {
         R.drawable.bokstavslottet01,
         R.drawable.bokstavslottet02,
         R.drawable.bokstavslottet03,
         R.drawable.bokstavslottet04,
         R.drawable.bokstavslottet05,
         R.drawable.bokstavslottet06,
         R.drawable.bokstavslottet07,
         R.drawable.bokstavslottet08,
         R.drawable.bokstavslottet09,
         R.drawable.bokstavslottet10,
         R.drawable.bokstavslottet11,
         R.drawable.bokstavslottet12,
         R.drawable.bokstavslottet13
        };

private Integer[] mImageIds1 = {
     R.drawable.i1,
        R.drawable.i2,
        R.drawable.i3,
        R.drawable.i4,
        R.drawable.i5,
        R.drawable.i6,
        R.drawable.i7,
        R.drawable.i8,
        R.drawable.i9,
        R.drawable.i10,
        R.drawable.i11,
        R.drawable.i12,
        R.drawable.i13
    };

private static String[] mTextNames = {
"BOOKSTOVEN1", "BOOKSTOVEN2", "BOOKSTOVEN3", "BOOKSTOVEN4", "BOOKSTOVEN5", "BOOKSTOVEN6", "BOOKSTOVEN7", "BOOKSTOVEN8", "BOOKSTOVEN9", "BOOKSTOVEN10", "BOOKSTOVEN11", "BOOKSTOVEN12", "BOOKSTOVEN13"};



private ArrayList<Integer> mPhotos = new ArrayList<Integer>(Arrays.asList(mImageIds));
private ArrayList<Integer> mPhotos1 = new ArrayList<Integer>(Arrays.asList(mImageIds1));

        }
4

2 回答 2

0

将您的活动设置为实现 OnClickListener 而不是 OnTouchListener

于 2012-04-17T09:36:58.453 回答
0

1.我想问你的第一件事是,你在这里提到了 1 个mPhotos阵列和剩下的三个mPhotos1。如果您在此代码中正确说明了为什么您需要不必要if的条件,则可以将 else 放在第一个if并提供mPhotos1剩余的数组(否则您不需要提供 2、3、4 值)。

  1. 下一个是你在这里做错了if(countk2==1)。怎么可能比较,因为你只通过额外的2k2

    if(countk1==1)
       {
        i.setImageResource(mPhotos.get(position));
       }
       if(countk2==1)
       {
        i.setImageResource(mPhotos1.get(position));
       }
       if(countk2==3)
       {
        i.setImageResource(mPhotos1.get(position));
       }
       if(countk2==4)
       {
        i.setImageResource(mPhotos1.get(position));
       }
    
  2. 然后,我认为您不需要在以下代码中放置具有不同名称的 Extra 值。

在这里,您正在独立接收k1, k2, k3,k4值。我觉得你不需要这样做。

     countk1 = getIntent().getIntExtra("k1", 0); 
     countk2 = getIntent().getIntExtra("k2", 0); 
     countk3 = getIntent().getIntExtra("k3", 0); 
     countk4 = getIntent().getIntExtra("k4", 0);





if(v==home1)
    {
        Intent i=new Intent(HomeDemo.this,Home1.class);
        i.putExtra("k1", 1);
        startActivity(i);
    }
    if(v==home2)
    {
        Intent i=new Intent(HomeDemo.this,Home1.class);
        i.putExtra("k2", 2);
        startActivity(i);
    }
    if(v==home3)
    {
        Intent i=new Intent(HomeDemo.this,Home1.class);
        i.putExtra("k3", 3);
        startActivity(i);
    }
    if(v==home4)
    {
        Intent i=new Intent(HomeDemo.this,Home1.class);
        i.putExtra("k4", 4);
        startActivity(i);
    }

除了执行上述操作之外,您还可以putExtra按照以下代码的意图实现。

if(v==home1)
{
    Intent i=new Intent(HomeDemo.this,Home1.class);
    i.putExtra("k", 1);
    startActivity(i);
}
if(v==home2)
{
    Intent i=new Intent(HomeDemo.this,Home1.class);
    i.putExtra("k", 2);
    startActivity(i);
}
if(v==home3)
{
    Intent i=new Intent(HomeDemo.this,Home1.class);
    i.putExtra("k", 3);
    startActivity(i);
}
if(v==home4)
{
    Intent i=new Intent(HomeDemo.this,Home1.class);
    i.putExtra("k", 4);
    startActivity(i);
}

然后你可以收到如下代码,

     count = getIntent().getIntExtra("k", 0); 

Adapter你可以按照这个,

   if(count==1)
   {
    i.setImageResource(mPhotos.get(position));
   }
   else if(count==2)
   {
    i.setImageResource(mPhotos1.get(position));
   }
   else if(count==3)
   {
    i.setImageResource(mPhotos2.get(position));
   }
   if(count==4)
   {
    i.setImageResource(mPhotos3.get(position));
   }

我希望你已经理解了上面的解释。

这里有一些有用的解决方案。

为了接收Extras 您还可以执行以下操作,

getIntent.getIntExtrta("k",0)不使用Bundle.

于 2012-04-17T10:05:54.407 回答