2017-04-24 95 views
-2

我正在處理我的主要項目,並且希望我的按鈕可以根據獲取的字符串數組值打開不同的意圖。我在按鈕的OnClickListener中使用if和else if語句,但不會再點擊。請幫幫我。如何在Android中按鈕的OnClickListener中添加if語句

這是我的XML文件:

<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    app:cardCornerRadius="3dp" 
    app:cardElevation="2dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="@dimen/spacing_large"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="@dimen/spacing_middle" 
     android:text="Syllabus" 
     android:textAppearance="@style/TextAppearance.AppCompat.Title" /> 

    <Button 
     android:id="@+id/buttonpdf" 
     style="@style/Widget.AppCompat.Button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     android:text="Open Book" /> 

</LinearLayout> 

這是相同的活動Java文件:

public class ActivityBookDetails extends AppCompatActivity { 

    public static final String EXTRA_OBJCT = "com.app.sample.recipe.OBJ"; 

    private Book book; 
    private FloatingActionButton fab; 
    private View parent_view; 

    Button button; 
    String[] obj; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(activity_book_details); 
     parent_view = findViewById(android.R.id.content); 
     button = (Button)findViewById(R.id.buttonpdf); 


     book = (Book) getIntent().getSerializableExtra(EXTRA_OBJCT); 
     fab = (FloatingActionButton) findViewById(R.id.fab); 
     fabToggle(); 
     setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setTitle(book.getName()); 

     ((ImageView) findViewById(R.id.image)).setImageResource(book.getPhoto()); 

     LinearLayout subjects = (LinearLayout) findViewById(R.id.subjects); 


     final String[] title_subjects = getResources().getStringArray(R.array.Subjects); 
     addIngredientsList(subjects, title_subjects); 
     obj = title_subjects; 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if ("Soft Computing".equals(obj)) { 
        Intent intent = new Intent(ActivityBookDetails.this, pdfviewactivity.class); 

        startActivity(intent); 
       } 
       else if ("Web Engineering".equals(obj)) { 
        Intent intent = new Intent(ActivityBookDetails.this, pdfweben.class); 

        startActivity(intent); 
       } 
       else if ("Network Management".equals(obj)) { 
        Intent intent = new Intent(ActivityBookDetails.this, pdfnetwork.class); 

        startActivity(intent); 
       } 
       else if ("Wireless Network".equals(obj)) { 
        Intent intent = new Intent(ActivityBookDetails.this, pdfwireless.class); 

        startActivity(intent); 
       } 
      } 
     }); 

我想我如果其他條件的值的基礎上開展工作字符串數組。

這是給定的意圖的一個樣本的Java文件:

public class pdfwireless extends AppCompatActivity { 
    PDFView pdfView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_pdfwireless); 
     pdfView = (PDFView)findViewById(R.id.pdfView); 
     pdfView.fromAsset("wirelessnetwork.pdf").load(); 
    } 
} 
+0

調試它並檢查'obj'的值 –

回答

1

你可能不應該嘗試添加里面的條件語句例如,最簡單的做法是將條件邏輯移動到另一個方法。

@Override 
public void onClick(View view){ 
    goToNextView(); 
} 

private void goToNextView(){ 
    if (title_subjects[0] == "Soft Computing") { 
     Intent intent = new Intent(ActivityBookDetails.this, pdfviewactivity.class); 
     startActivity(intent); 
    } 
    // else if {} 
    // else {} 
} 

由於cricket_007已經建議有比使用if/else if語句更好的方法。如果您計劃添加更多選項,可以考慮使用枚舉或映射。

public enum Subjects { 
    SOFT_COMPUTING("Soft Computing", pdfviewactivity.class), 
    WEB_ENGINEERING("Web Engineering", pdfweben.class), 
    NETWORK_MANAGEMENT("Wireless Network", pdfnetwork.class), 
    WIRELESS_NETWORK("Network Management", pdfwireless.class); 

    private String name; 
    private Class clazz; 

    Subjects(String name, Class clazz){ 
     this.name = name; 
     this.clazz = clazz; 
    } 

    public static Class getClass(String title_subject) { 
     for(Subjects subject: Subjects.values()) { 
      if (subject.name.equals(title_subject)) { 
       return subject.clazz; 
      } 
     } 
     return null; 
    } 
} 

private void goToNextView() { 

    //Alternative to conditionals using Enum 
    Class theClassToGoTo = Subjects.getClass(title_subjects[0]); 
    Intent intent = new Intent(ActivityBookDetails.this, theClassToGoTo); 
    startActivity(intent); 

    //Alternative to conditionals using HashMap 
    Map<String, Class> subject_map = new HashMap<>(); 
    subject_map.put("Soft Computing", pdfviewactivity.class); 
    subject_map.put("Web Engineering", pdfweben.class); 
    subject_map.put("Wireless Network", pdfnetwork.class); 
    subject_map.put("Network Management", pdfwireless.class); 

    Class theClassToGoTo2 = subject_map.get(title_subjects[0]); 
    Intent intent2 = new Intent(ActivityBookDetails.this, theClassToGoTo2); 
    startActivity(intent); 
} 

最簡單的變化是使用HashMap的,雖然這樣做的缺點是goToNextView方法需要改變每添加新的課題時間。Enum涉及更多的代碼,但更簡單一點,因爲所有東西都有一個改變的目的。

還有其他的方法可以做到這一點,以上只是兩個簡單的建議,不涉及改變太多現有的代碼。

0

讓我們來看看...

final String[] title_subjects = getResources().getStringArray(R.array.Subjects); 
obj = title_subjects; 

而且......

if ("Soft Computing".equals(obj)) { 

(注: obj變量在這裏沒有意義)

那麼,String永遠不會等於String[],所以存在這個問題。


你要麼需要

1)獲得這個數組個人要比較的字符串,

2)獲取的R.string資源,而不是一個R.array

3)也許使用一些boolean contains(String[] strings, String findMe)方法的實現。但是,我不知道這是否適用於您所顯示的邏輯

4)使用其他更有意義的條件......也許你有book.getSubject()


選項1,例如...

button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     String s0 = title_subjects[0]; // Get just the first string 

     Class clz = null; 

     if ("Soft Computing".equals(s0)) { 
      clz = pdfviewactivity.class; 
     } else if ("Web Engineering".equals(s0)) { 
      clz = pdfweben.class; 
     else if ("Network Management".equals(s0)) { 
      clz = pdfnetwork.class; 
     } else if ("Wireless Network".equals(s0)) { 
      clz = pdfwireless.class; 
     } 

     if (clz != null) { 
      Intent intent = new Intent(ActivityBookDetails.this, clz); 
      startActivity(intent); 
     } 
    } 
}); 

使用Map<String, Class>看起來不是很長的if-else語句清潔