我有一个listOf()模型SettingsItemViewModel
并且根据条件,我必须添加另一个模型,我将在此列表中显示。
我执行以下操作
return if (condition == true) listItems + SettingsItemViewModel(title) else listItems
但是在这里我将模型添加到列表的末尾,我怎么能把它放在第三位呢?
我有一个listOf()模型SettingsItemViewModel
并且根据条件,我必须添加另一个模型,我将在此列表中显示。
我执行以下操作
return if (condition == true) listItems + SettingsItemViewModel(title) else listItems
但是在这里我将模型添加到列表的末尾,我怎么能把它放在第三位呢?
我编写了一个具有以下依赖项的库:
implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.2'
在我的项目中,我决定添加这个库:
implementation 'com.mandarine.sdk:mandarine-library:2.0.1@aar'
但是由于在我的项目中我使用了与库中相同的依赖项,因此我决定将它们从主项目中删除并仅引用库中的那些,将库更改implementation为api. 我重建了这个库,更新了这个库在项目中的依赖,但是唉,项目不能使用这个库中的依赖。
我得到这样的东西: https ://gist.github.com/mnewlive/e0269a1f1b2a5062c6c16beb8329d268
清理项目,重建,我都试过了。我不明白catch是什么,因为实际上它应该这样工作,因为如果我通过一个模块添加我的库并build.grade在项目中编写以下内容:
implementation project(path: ':mandarine-library')
这一切都有效。
任务如下:
对于给定的 0 < n < 200,被视为一个人的年龄,返回一个字符串,如:“21 岁”、“32 岁”、“12 岁”。
写了这样的东西:
fun ageDescription(age: Int): String {
return when {
age / 10 % 10 == 1 -> "$age лет"
age % 10 == 1 -> "$age год"
age / 10 % 10 == 9 -> "$age лет"
else -> "$age года"
}
}
测试成功通过,顺便说一下:
@Test
fun ageDescription() {
assertEquals("1 год", ageDescription(1))
assertEquals("21 год", ageDescription(21))
assertEquals("132 года", ageDescription(132))
assertEquals("12 лет", ageDescription(12))
assertEquals("111 лет", ageDescription(111))
assertEquals("199 лет", ageDescription(199))
}
是否可以简化此决定?然后似乎这条线是多余的:age / 10 % 10 == 9 -> "$age лет"
PS 复数形式(few, many, other, etc..),不适合 Android :)
我遇到了以下问题,我连接了一个库,其中有一些具有特定 ID 的视图。并且在主项目中,当引用这个特定的视图时,不要为你的视图分配相同的 id。之后,kotlinx我没有在课堂上引用我的项目 id,而是找到了连接库的 id。
如何避免这种情况?到目前为止,作为一个选项,这是将 id 重命名为 main 或 library,但由于有时不会显式弹出,我想以某种不同的方式解决它。也许有人已经遇到过类似的事情。
更新:
我想注意导入不会改变,例如:
import kotlinx.android.synthetic.main.dialog_immediate_categorization.*
标记本身的位置:
<com.mandarine.android.common.views.TitleSwitchView
android:id="@+id/switchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleText="@string/v2_mobile_categories_assign_all_enable" />
如您所见,在哪里:com.mandarine.android.common在我的情况下,这是连接库的一个元素,其中TitleSwitchView选择了包含TextView和SwitchView其中为类指定 id的标记switchView。并且已经在我的课堂上,它不是指switchView类型,而是指类型TitleSwitchView的 id 。switchViewSwitchView
一首小曲:
String什么类型,最重要的是为什么x?
val x = null + null
有一个虚拟键盘(我的自定义键盘),我怎样才能让它不可点击?我试过了keyboardView.isEnabled,keyboardView.isEnabled = false可惜,它仍然会发出咔哒声。
我决定从我的笔记本电脑上卸下硬盘驱动器并将其重新安装到我的固定电脑上。不是作为闪存驱动器/驱动器,而是运行操作系统。但是当计算机启动时,它会弹出“硬件监视器...”窗口,该窗口描述了电压和其他一些系统状态。
我的硬盘驱动器上有两个操作系统(Windows 和 Ubuntu),如果在笔记本电脑启动时出现一个窗口,选择需要启动的操作系统,那么在计算机上它不会加载超出“硬件监视器...”窗口。怎样成为?
有一个 TextView,我在其中放置了 3 个单词。3 个词的长度很好,但你需要将它们排成一行。在xml中我写了以下参数:
android:maxLines="1"
android:ellipsize="end"
总体满意,但不完全满意。例如,如果第三个单词不完整,则在第二个单词之后有一个省略号
家电
而且这基本上是正常的,只是前两个字比较短。例如
货物和...
我想把它转换成这样的东西:
货物与服务...
以便将下一个单词的有效部分放置在给定的大小中。
有两种方法:showSystemKeyboard和hideSystemKeyboard。我认为到目前为止一切都是合乎逻辑的,一种方法显示键盘,另一种方法隐藏它。
我在 onStart() 方法中调用键盘显示showSystemKeyboard方法:
@Override
public void onStart() {
super.onStart();
if (allAvailableTags.isEmpty() && selectedTags.isEmpty()) {
Utils.showSystemKeyboard(tagNameInputView);
}
}
方法本身showSystemKeyboard:
public static void showSystemKeyboard(EditText view) {
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
任务是在我的 TagDialog 类中调用对话框时,其中实现了 onStart () 方法,键盘立即打开。我会立即进行预订,SHOW_FORCED 常量不会立即适合。谁在乎为什么,跟着这里。
试过这样的:
tagNameInputView.requestFocus();
tagNameInputView.postDelayed(new Runnable() {
@Override
public void run() {
if (allAvailableTags.isEmpty() && selectedTags.isEmpty()) {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(tagNameInputView, 0);
}
}
},200);
tagNameInputView.requestFocus();
tagNameInputView.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(tagNameInputView.getWindowToken(), 0);
}
},200);
但是我仍然必须在键盘打开之前单击该字段。
在onStart我显示键盘的方法中:
if (allTags.isEmpty()) {
Utils.showSystemKeyboard(true, tagNameInputView);
}
但是,当您使用主页按钮退出应用程序时,键盘仍保留在显示屏上。如何正确销毁所有不必要的元素,在本例中为键盘。我尝试这种方式,但在中文手机上,键盘不会移除,这与 nexus'a 不同。
@Override
public void onStop() {
super.onStop();
Utils.hideSystemKeyboard(tagNameInputView);
}
键盘隐藏方法:
public static void hideSystemKeyboard(EditText view) {
if (view != null) {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
检查后git status,我查看了修改后的文件。但是你如何单独选择它们呢?- 我会解释的。我想将 9 个文件分成 3 个提交,每个提交有 n 个文件。
我有如果
setupFragment(getString(R.string.ui_data_collection_accounts), true, FabAction.ADD_ACCOUNT);
在其他方面
setupFragment(getString(R.string.ui_data_collection_accounts), true, FabAction.NO_ACTION);
我试图将它写在一行中,因为这里只有 FabAction 的值发生变化,是否可以使用三元运算符?
在我的应用程序中,会立即对整个列表执行查询,我怎样才能让它们像在链中一样运行?首先对第一个元素执行,完成后开始对第二个元素执行。
private void loadJSON() {
Call<List<Integer>> call = request.getTopStories();
call.enqueue(new Callback<List<Integer>>() {
@Override
public void onResponse(Call<List<Integer>> call, Response<List<Integer>> response) {
topStories = response.body();
loadDetails();
}
@Override
public void onFailure(Call<List<Integer>> call, Throwable t) {
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show();
}
});
}
private void loadDetails(){
for (Integer id : topStories) {
request.getTopStore(id).enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> response) {
models.add(response.body());
topStoriesAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Model> call, Throwable t) {
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show();
}
});
}
}
有一个带有一些值的 recyclerView 列表,当您转到某些元素открывается时,有关此元素的信息。如何使它在打开一个元素后改变列表中的颜色。
public class TopStoriesAdapter extends RecyclerView.Adapter<TopStoriesAdapter.ViewHolder> {
private List<Model> topStoriesList;
private Context context;
public TopStoriesAdapter(List<Model> topStoriesList, Context context) {
this.context = context;
this.topStoriesList = topStoriesList;
}
@Override
public TopStoriesAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
return new TopStoriesAdapter.ViewHolder(itemView);
}
@Override
public void onBindViewHolder(final TopStoriesAdapter.ViewHolder viewHolder, int i) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
viewHolder.storyTitle.setText(topStoriesList.get(i).getTitle());
viewHolder.storyCreator.setText(topStoriesList.get(i).getBy());
viewHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, TopStoryDetailActivity.class);
intent.putExtra("key", topStoriesList.get(viewHolder.getAdapterPosition()));
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return topStoriesList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.storyTitle)
TextView storyTitle;
@BindView(R.id.storyCreator)
TextView storyCreator;
public ViewHolder(View view) {
super(view);
storyTitle = (TextView) view.findViewById(R.id.storyTitle);
storyCreator = (TextView) view.findViewById(R.id.storyCreator);
this.view = view;
}
}
}
我找到了一个类似的例子,但我不能将它应用到我的适配器上。
在谷歌地图中,我想实现 myCurrentLocation 按钮。在 onCreate 方法中添加了以下内容:
googleMap.setMyLocationEnabled(true);
并且在AndroidManifest'e 中指定了必要permission的 ':
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在尝试运行该项目时,我遇到了以下 2 个错误:
错误:.dex文件中的方法引用数不能超过64K。在https://developer.android.com/tools/building/multidex.html了解如何解决此问题
错误:任务 ':app:transformClassesWithDexForOkhttpDebug' 执行失败。com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:java.util.concurrent.ExecutionException:com.android.ide.common.process.ProcessException:org.gradle.process。 internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_60\bin\java.exe'' 以非零退出值 2 完成
在我用谷歌搜索我的第一个错误后,我在官网上发现了一个有趣的恭喜:
如果您构建了一个 Android 应用程序并收到此错误,那么恭喜您,您有很多代码!
但是如果我的应用其实没有那么繁琐,明显不需要连接multidex呢。
我正在尝试在我的活动中实现地图,但是当从类别切换到活动时,地图不会加载:
地图活动类
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap googleMap;
private static final double TARGET_LATITUDE = 17.893366;
private static final double TARGET_LONGITUDE = 19.511868;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
createMapView();
}
private void createMapView() {
// MapsInitializer.initialize(context);
try {
if (null == googleMap) {
((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMapAsync(this);
if (null == googleMap) {
Toast.makeText(getApplicationContext(),
"Error creating map", Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception) {
Log.e("mapApp", exception.toString());
}
}
private void addMarker() {
double lat = TARGET_LATITUDE;
double lng = TARGET_LONGITUDE;
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(lat, lng))
.zoom(15)
.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.animateCamera(cameraUpdate);
if (null != googleMap) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Mark")
.draggable(false)
);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
addMarker();
}
}
在此之前,代码本身存在错误,
无法解析方法
getMap
在代码部分:
try {
if(null == googleMap){
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
但我决定将其替换getMap为getMapAsync,并实施OnMapReadyCallback. 也许这就是错误的原因。
激活码layout:
<?xml version="1.0" encoding="utf-8"?>
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在一个片段中,我试图访问我的应用程序中的特定类。
private void initializeAdapter() {
Adapter adapter = new Adapter(persons, new Adapter.OnRVItemClickListener(){
@Override
public void onRVItemClick(int position) {
Intent intent = null;
switch(position){
case 0:
intent = new Intent(getActivity(), MainActivity.class);
break;
case 1:
intent = new Intent(getActivity(), MainActivity.class);
break;
default:
intent = new Intent(getActivity(), CustomIntro.class);
break;
}
getActivity().startActivity(intent);
}
});
recyclerView.setAdapter(adapter);
}
给出以下错误:
除了片段之外,我还想在我的导航抽屉中为活动添加一个过渡
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
if (id == R.id.nav_camera) {
fragment = new MainFragment();
} else if (id == R.id.nav_gallery) {
fragment = new SecondFragment();
} else if (id == R.id.nav_slideshow) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
如果在遍历片段时没有问题,那么在转到 MainActivity 时会抛出错误
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference