2017-08-26 138 views
0

(使用android工作室) 問題是當我在真實設備上運行我的應用程序時,它太慢了,而在模擬器上它速度很快,我看到我必須擴展asynctask在我的課上或在最慢的代碼部分創建一個新的線程,但即時通訊新的,我不知道該怎麼做,我試圖創建新的線程,當用戶點擊按鈕,但它沒有工作,所以如果有人可以幫助我會感激它。 這只是主要活動,因爲id就像知道如何去做,而且我會在理解後改變其他類。Android應用程序在真實設備上非常慢

順便說一句,在XML文件我有3個按鈕,1個列表視圖,1個文本視圖。

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(); 
    } 

} 


} 

回答

相關問題