确定从另一个类中单击了哪个项目

时间:2013-02-12 23:58:13

标签: android bundle viewflipper

我有一个List类和一个Viewflipper类,如何知道在Viewflipper类的List类中点击了哪个Item,如果可能的话,我还想将每个视图链接到Viewflipper的特定视图。这是我到目前为止所尝试的:

列表类:

public class List extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // storing string resources into Array
    String[] service_list = getResources().getStringArray(R.array.List);

    // Binding resources Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list, R.id.list, service_list));

}

@Override
protected void onListItemClick(ListView parent, View v, int position, long id) {
    // TODO Auto-generated method stub
     String name = (String) parent.getItemAtPosition(position); 
     //Toast.makeText(this, "you have clicked " + name, Toast.LENGTH_SHORT).show();


    switch (position) {
    case 0:
        Intent i = new Intent(List.this, Viewflipper.class);
        Bundle b = new Bundle();
        b.putString("a", "apple");
        i.putExtras(b);
        startActivity(i);
        break;

    case 1:
        Intent a = new Intent(List.this, Viewflipper.class);
        Bundle aa = new Bundle();
        aa.putString("b", "banana");
        a.putExtras(aa);
        startActivity(a);

        break;

    case 2:
        Intent ab = new Intent(List.this, Viewflipper.class);
        Bundle bb = new Bundle();
        bb.putString("m", "mango");
        ab.putExtras(bb);
        startActivityForResult(ab, 2);
        break;

    case 3:
        Intent c = new Intent(List.this, Viewflipper.class);
        Bundle cc = new Bundle();
        cc.putString("o", "orange");
        c.putExtras(cc);
        startActivity(c);
         break;
    default:
        break;
    }

    super.onListItemClick(parent, v, position, id);
}
} 

ViewFlipper类:

      public class Viewflipper extends Activity implements OnClickListener,
      OnGestureListener {

private ViewFlipper viewflipper;
private Button btnNext, btnPrevious;
private GestureDetector gesturedetector = null;

TextView txt;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewflipper);

    viewflipper = (ViewFlipper) findViewById(R.id.viewSwitcher);
    btnNext = (Button) findViewById(R.id.btnNext);
    btnPrevious = (Button) findViewById(R.id.btnPrevious);
    txt = (TextView) findViewById(R.id.txtview);

    txt.setOnClickListener(this);
    btnNext.setOnClickListener(this);
    btnPrevious.setOnClickListener(this);

    gesturedetector = new GestureDetector(this, this);

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {



        if (bundle.getString("a").equals("apple")) {
            Toast.makeText(this, " the first Item was clicked ",
                    Toast.LENGTH_LONG).show();
        } else if (bundle.getString("b").equals("banana")) {
            Toast.makeText(this, "the second Item was clicked",
                    Toast.LENGTH_LONG).show();
        } else if (bundle.getString("m").equals("mango")) {
            Toast.makeText(this, "the fourth Item was clicked",
                    Toast.LENGTH_LONG).show();
        } else if (bundle.getString("o").equals("orange")) {
            Toast.makeText(this, "the fifth Item was clicked",
                    Toast.LENGTH_LONG).show();
        }

        else {


        } // Toast.makeText(this, b , Toast.LENGTH_SHORT).show() ;
    }
}
// Animation
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnNext:
        // Button Next Style
        Animation animationNext = AnimationUtils.loadAnimation(this,
                R.anim.btn_style_next);
        btnNext.startAnimation(animationNext);

        nextView();
        break;

    case R.id.btnPrevious:
        // Button Previous Style
        Animation animationPrevious = AnimationUtils.loadAnimation(this,
                R.anim.btn_style_previous);
        btnPrevious.startAnimation(animationPrevious);

        previousView();
        break;

    case R.id.txtview:
        Toast.makeText(this, "dail contact or visit website",
                Toast.LENGTH_SHORT).show();
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return gesturedetector.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

int SWIPE_MIN_VELOCITY = 100;
int SWIPE_MIN_DISTANCE = 100;

// Sự kiện khi bạn vuốt màn hình đưa sang một bên nào đó
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // Get Position
    float ev1X = e1.getX();
    float ev2X = e2.getX();

    // Get distance of X (e1) to X (e2)
    final float xdistance = Math.abs(ev1X - ev2X);
    // Get veclocity of cusor
    // Vận tốc = số điểm ảnh (px) / giây
    final float xvelocity = Math.abs(velocityX);

    // Vận tốc chuyển đổi X > 100 và khoảng cách từ điểm kéo đầu đến điểm
    // kéo cuối > 100
    if ((xvelocity > SWIPE_MIN_VELOCITY)
            && (xdistance > SWIPE_MIN_DISTANCE)) {
        if (ev1X > ev2X)// Switch Left
        {
            previousView();
        } else// Switch Right
        {
            nextView();
        }
    }

    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

// Next, Previous Views
private void previousView() {

    // Previous View
    viewflipper.setInAnimation(this, R.anim.in_animation1);
    viewflipper.setOutAnimation(this, R.anim.out_animation1);
    viewflipper.showPrevious();

}

private void nextView() {

    // Next View
    viewflipper.setInAnimation(this, R.anim.in_animation);
    viewflipper.setOutAnimation(this, R.anim.out_animation);
    viewflipper.showNext();

}

}

请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

首先,我建议您更改List代码,如下所示:

switch (position) {
Intent i = new Intent(List.this, Viewflipper.class);
case 0:
    i.putExtra ("key", "apple");
    startActivity(i);
    break;

case 1:
    i.putExtra ("key", "banana");
    startActivity(i);
    break;

case 2:
    i.putExtra ("key", "mango");
    startActivity(i);
    break;

case 3:
    i.putExtra ("key", "orange");
    startActivity(i);
    break;
default:
    break;
}

然后在Viewflipper中,更改您的代码,使其如下所示:

if (getIntent().hasExtra("key")) {

String extra = getIntent().getStringExtra ("key");

    if ("apple".equals (extra)){
        Toast.makeText(this, " the first Item was clicked ",
                Toast.LENGTH_LONG).show();
    } else if ("banana".equals (extra)){
        Toast.makeText(this, "the second Item was clicked",
                Toast.LENGTH_LONG).show();
    } else if  ("mango".equals (extra)){
        Toast.makeText(this, "the fourth Item was clicked",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "the fifth Item was clicked",
                Toast.LENGTH_LONG).show();
    }
}

我改变了什么:

  • 在切换开始时声明Intent,以保存一些代码。
  • 这样做,以便额外传递总是有相同的键。你之前在做什么(每个案例的一个单独的密钥不能很好地工作,你可以获得NPE,它会击败键值对。
  • 远离Bundle,因为它是多余的。 Intent#putExtra()Intent#getStringExtra()没问题。
  • "string".equals (stringVariable)方法String#equals()以来,null方法可以处理NPE值而不会抛出{{1}}。