(我正在使用android studio) 问题是当我在真实设备上运行我的应用程序它是如此之慢,而在模拟器上它是相当快,我看到我必须在我的类上扩展asynctask或在代码的最慢部分创建一个新线程,但我新的,我不知道该怎么做,我试图创建新的线程,当用户点击按钮,但它没有工作,所以如果有人可以帮助我会欣赏它。 这只是主要活动,因为id就像知道如何做到这一点,我会在理解之后更改其他类。
顺便说一句,在xml文件中我有3个按钮,1个listview,1个textview。
package com.wog.playerslist;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String itemName;
int itemPosition;
ArrayList<String> player = new ArrayList<>(4);
ArrayList<String> playersList = new ArrayList<>(50);
ArrayAdapter<String> playersAdapter;
ListView playersListView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playersListView = (ListView) findViewById(R.id.listViewPlayers);
playersListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setItemName(playersListView.getItemAtPosition(position).toString());
setItemPosition(position);
}
});
}
@Override
public void onResume() {
super.onResume();
}
public void onButtonAddClicked(View view) {
Intent intent = new Intent(this, PlayerAddActivity.class);
startActivity(intent);
}
@Override
protected void onNewIntent(Intent intent) {
if (intent != null)
setIntent(intent);
Bundle extras = this.getIntent().getExtras();
if (extras != null) {
if (extras.getString("Edit") != null) {
playersList.remove(itemPosition);
playersAdapter.notifyDataSetChanged();
}
String player;
player = extras.getString("Nick") + " - " + extras.getString("Tag") + " - " + extras.getString("Vic") + " - " + extras.getString("Def");
playersList.add(player);
playersAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, playersList);
playersListView.setAdapter(playersAdapter);
playersAdapter.notifyDataSetChanged();
}
}
public void onRemoveBtnClicked(View view) {
if (itemName != null) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Delete");
alertDialog.setMessage("Delete " + itemName + "?");
alertDialog.setNegativeButton("Cancel", null);
alertDialog.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
playersList.remove(itemPosition);
playersAdapter.notifyDataSetChanged();
itemName = null;
itemPosition = -1;
}
});
alertDialog.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Please, select a player!", Toast.LENGTH_SHORT);
toast.show();
}
}
public void setItemName(String str) {
this.itemName = str;
}
public void setItemPosition(int i) {
this.itemPosition = i;
}
public void onEditButtonClicked(View view) {
int i;
int x = 0;
int add = 0;
Intent intent = new Intent(this, PlayerAddActivity.class);
if (itemName != null) {
while (add <= 2) {
i = itemName.indexOf(" - ", x);
player.add(add, itemName.substring(x, i));
x = i + 3;
add++;
}
player.add(add, itemName.substring(x, itemName.length()));
intent.putExtra("Edit", "editView");
intent.putExtra("Nick", player.get(0));
intent.putExtra("Tag", player.get(1));
intent.putExtra("Vic", player.get(2));
intent.putExtra("Def", player.get(3));
startActivity(intent);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Please, select a player!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
答案 0 :(得分:0)
根据设计,新的Android Emulator在大多数情况下比物理Android设备更快。慢度还取决于您使用的手机类型,外部库,以及最重要的编码技术。查看此YouTube链接以获取更多信息https://www.youtube.com/watch?v=fs0eira2pRY&feature=youtu.be&t=27m42s