gpt4 book ai didi

java - ListView asList 无法被 android studio 识别

转载 作者:行者123 更新时间:2023-11-30 00:36:23 25 4
gpt4 key购买 nike

构建 ListView 并从下面的代码开始工作,但正如突出显示的那样,我的 asList 无法被 Java 识别。有人可以告诉我我哪里出错了。这适用于以前的 android studio 版本,但现在不行?

package com.example.air rocketapps.listviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import static java.util.Arrays.asList;


public class MainActivity extends Activity {

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

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

final ArrayList<String> myFriends = new ArrayList<String>(**asList**("John", "Paul", "George", "Ringo"));

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myFriends);

friendsListView.setAdapter(arrayAdapter);

friendsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Toast.makeText(getApplicationContext(), "Hello " + myFriends.get(position), Toast.LENGTH_LONG).show();

}
});

}

@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_main, 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);
}
}

最佳答案

Arrays.asList()是一种结构方法,所以你不应该使用 new ArrayList用它只需使用 final ArrayList<String> myFriends = Arrays.asList("John", "Paul", "George", "Ringo"));

更新:最好在赋值的左侧使用界面,即

最终列表 myFriends

关于java - ListView asList 无法被 android studio 识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43349014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com