Android - 基于轮播选择启动意图(onItemSelected)

时间:2014-04-15 06:11:55

标签: android android-intent

我试图修改一些显示吐司的代码,当在下面的图片轮播中选择了几个项目中的一个时:

https://code.google.com/p/android-3d-carousel-view/source/browse/src/net/pocketmagic/android/carousel/MainActivity.java

public void afterTextChanged(Editable arg0) {}

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        m_carouselAdapter.getFilter().filter(s.toString()); 
    }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
         CarouselDataItem docu =  (CarouselDataItem) m_carouselAdapter.getItem((int) arg3);
         if (docu!=null)
             Toast.makeText(this, "You've clicked on:"+docu.getDocText(), Toast.LENGTH_SHORT).show();
    }

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



}

我想根据选择的项目来修改此项以启动意图

例如:

  if (arg== 1){
          Intent intent = new Intent(MyActivity.this, Activity1.class);
          MyActivity.this.startActivity(intent);
      } else if { (arg== 2){
          Intent intent = new Intent(MyActivity.this, Activity2.class);
          MyActivity.this.startActivity(intent);
      }}

如何实现这一目标?

完整来源: https://code.google.com/p/android-3d-carousel-view/

3 个答案:

答案 0 :(得分:0)

     public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // display view for selected item
    displayView(position);
}

     private void displayView(int position) {
// update the main content with called Fragment
switch (position) {


case 1:
    //home();
    Intent i = new Intent(this,Design_Activity.class);
    startActivity(i);
    this.finish();
    break;
case 2:
    //profile();
    Intent intent = new Intent(this,Profile.class);
    startActivity(intent);
    this.finish();
    break;
case 3:
    //credit();
    Intent intent1 = new Intent(this,Credits.class);
    startActivity(intent1);
    this.finish();
    break;
case 4:
    //Logout();
    Intent z = new Intent(this,MainActivity.class);
    z.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
              Intent.FLAG_ACTIVITY_CLEAR_TASK |
              Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(z);
        this.finish();
    break;

default:
    break;
}

试试这段代码希望这对你有用..

答案 1 :(得分:0)

我认为这可能对您有帮助,请在点击事件中使用此代码。

final Intent intent = new Intent(Intent.ACTION_VIEW);
      Uri uri =Uri.fromFile(file);
      intent.setDataAndType(uri, mimetypeOfFile);
      startActivity(intent); 

这里你得到你的文件的mimetype,可以检索

  MimeTypeMap mimeType = MimeTypeMap.getSingleton();
  mimeType.getMimeTypeFromExtension(fileExtension);

fileExtension 就像 .txt 一样用于**文本电影

如果有任何混淆,Plz让我知道

答案 2 :(得分:0)

尝试类似 -

    textView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
        String name = ((TextView) view).getText().toString();
        Intent intent = null;

        if (name .equals("John F. Kennedy"))
            intent = new Intent (MainActivity.this, KennedyActivity.class);
        if (name .equals("Lyndon B. Johnson"))
            intent = new Intent (MainActivity.this, JohnsonActivity.class);
        if (name .equals("Richard Nixon"))
            intent = new Intent (MainActivity.this, NixonActivity.class);
        if (name .equals("Gerald Ford"))
            intent = new Intent (MainActivity.this, FordActivity.class);
        if (president.equals("Jimmy Carter"))
            intent = new Intent (MainActivity.this, CarterActivity.class);
        if (name .equals("Ronald Reagan"))
            intent = new Intent (MainActivity.this, ReaganActivity.class);
        if (name .equals("George H. W. Bush"))
            intent = new Intent (MainActivity.this, SrBushActivity.class);
        if (name .equals("Bill Clinton"))
            intent = new Intent (MainActivity.this, ClintonActivity.class);   
        if (intent != null) {
            intent.putExtra("KEY", name );
            startActivity(intent);
        }
    }
});