2015-11-06 31 views
0

我正在製作一個非常簡單的應用程序,它顯示100個隨機數字,當在列表視圖中單擊一個數字時,它將啓動一個Web瀏覽器。我非常肯定,我已經完成了所有工作,但是無論如何要將相同的意圖綁定到列表中的每個數字/位置?除了switch語句?如果需要,我會做一個switch語句,但是100個數字是很多的。哈哈。對於我的生活,我無法弄清楚該怎麼做。一個循環會起作用嗎?我只用了約兩個月的Android Studio。感謝您提供的所有幫助。Android Studio列表視圖,意圖

@Override 
public void onItemClick(AdapterView<?> parent, View v, int position, long id){ 

    switch (position) { 
     case 0: String link = "https://duckduckgo.com/"; 
      Uri uri = Uri.parse(link); 
      Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri); 
      startActivity(viewIntent); 
    } 

    String link = "https://duckduckgo.com/"; 
    Uri uri = Uri.parse(link); 


    Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri); 
    startActivity(viewIntent); 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_assignment4); 

    ListView ListViewNumbers = (ListView) findViewById(R.id.ListViewNumbers); 


    Random ran = new Random(); 
    List<Integer> nums = new ArrayList<>(100); 
     for(int i = 0; i < 100; i++){ 
      Integer r = ran.nextInt(); 
      nums.add(r); 
     } 

    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, R.layout.num_list_layout, nums); 
    ListViewNumbers.setAdapter(adapter); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_assignment4, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

+0

你的意思是每個項目有不同的URI? – starkshang

+0

每個項目可以有相同的uri,我只需要一種方法來設置所有100個號碼的意圖,而不必輸入100個個案。哈哈。 – BHP90

+0

如果意圖是相同的,爲什麼不建立一個意向性的進一步並在onItemClick上啓動它。 – starkshang

回答

0

你爲什麼不以你的交換機的情況下使用默認?這裏是你的代碼

switch (position) { 
     case 0: String link = "https://duckduckgo.com/"; 
      Uri uri = Uri.parse(link); 
      Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri); 
      startActivity(viewIntent); 
      break; 
     default: 
       String link = "https://duckduckgo.com/"; 
      Uri uri = Uri.parse(link); 
      Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri); 
      startActivity(viewIntent); 
      break; 
    } 
+0

這是否會工作,以便無論點擊列表視圖上的哪個項目,它都會轉到意圖? – BHP90

+0

嘗試一次..有什麼可以鬆動的? –

0

如何在strings.xml聲明string-array那麼你可以把它作爲String[]並將其分配給您的adapter項目。我認爲這很容易。

<string-array name="uri_list"> 
    <item>uri1</item> 
    <item>uri2</item> 
    .... 
    <item>uri100</item> 
</string-array> 
0

您聲明在strings.xml 每個項目string-array是一個URL。並且依靠onItemClick的位置,可以從字符串數組中提取url。

0

試試這個

MyListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {// put de code if you intent here thats all } });