我正在尝试制作一个带有制表符的程序,每个制表符代表一个片段。在第二个片段(AddFragment)中,我想制作一个列表来显示我在EditText中输入的字符串。但是它给了我上面的错误。更多详细信息:
error: cannot infer type arguments for ArrayAdapter<>
itemsAdapter = new ArrayAdapter<>(AddFragment.this, android.R.layout.simple_list_item_1, items);
AddFragment类别的程式码:
public class AddFragment extends Fragment {
EditText etPlan;
EditText etExercise;
ImageButton imageButton;
ArrayList<String> items;
ArrayAdapter<String> itemsAdapter;
ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_add, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
findViews();
super.onViewCreated(view, savedInstanceState);
}
private void findViews() {
listView = getView().findViewById(R.id.listView);
etPlan = getView().findViewById(R.id.etPlan);
etExercise = getView().findViewById(R.id.etExercise);
imageButton = getView().findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
items = new ArrayList<>();
itemsAdapter = new ArrayAdapter<>(AddFragment.this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(itemsAdapter);
setUpListViewListener();
}
private void setUpListViewListener() {
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Context context = getContext();
Toast.makeText(context, "Item Removed", Toast.LENGTH_LONG).show();
items.remove(position);
itemsAdapter.notifyDataSetChanged();
return true;
}
});
}
});
}
}
1条答案
按热度按时间u59ebvdq1#
如果直接在Activity JAVA文件中工作,您拥有的声明就可以工作。在片段中工作时,您需要清楚地建立参数的类型并提供正确的上下文。
下面的代码应该可以工作(假设您正在传递字符串):