我正在製作一個非常簡單的應用程序,它顯示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);
}
}
你的意思是每個項目有不同的URI? – starkshang
每個項目可以有相同的uri,我只需要一種方法來設置所有100個號碼的意圖,而不必輸入100個個案。哈哈。 – BHP90
如果意圖是相同的,爲什麼不建立一個意向性的進一步並在onItemClick上啓動它。 – starkshang