import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashMap;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private ArrayList LocalList = new ArrayList(5);
private HashMap LocalListDetail = new HashMap
private int addLocalList(String dev_name, String Addr) {
int index = -1;
boolean isDuplicate = false;
isDuplicate = LocalList.contains(dev_name);
if (LocalList.isEmpty()){
LocalList.add(dev_name);
}else if ((!LocalList.isEmpty())&& (!isDuplicate))
{
if (LocalList.size() == 1)
{
LocalList.add(dev_name);
Collections.reverse(LocalList);
}
else
{
isDuplicate = LocalList.contains(dev_name);
Collections.reverse(LocalList);
LocalList.add(dev_name);
Collections.reverse(LocalList);
index = LocalList.indexOf(dev_name);
}
}
isDuplicate = LocalListDetail.containsKey(dev_name);
if (!isDuplicate) {
LocalListDetail.put(dev_name, Addr);
}
Log.v("LOG", "Added success, start to query all local list.");
queryAllLocalList();
return index;
}
private void deleteLocalList(int key_position)
{
int index = -1;
String getResult = "";
boolean isContains = false;
if((key_position >= 0) && (key_position <= 4))
{
getResult = (String)LocalList.get(key_position);
isContains = LocalListDetail.containsKey(getResult);
if(isContains)
LocalListDetail.remove(getResult);
try {
LocalList.remove(key_position);
Log.v("LOG", "Delete " + key_position + "th data success");
}catch(Exception e)
{
Log.v("LOG", "Delete enter into try catch exception");
}
}
Log.v("LOG", "Start to query all local list.");
queryAllLocalList();
}
public void modifyLocalList(int key_position, String dev_name, String Addr)
{
int index = -1;
String getResult = "";
boolean isContains = false;
if((key_position >= 0) && (key_position <= 4))
{
getResult = (String)LocalList.get(key_position);
isContains = LocalListDetail.containsKey(getResult);
if(isContains) {
LocalListDetail.remove(getResult);
LocalListDetail.put(dev_name, Addr);
}else{
LocalListDetail.put(dev_name, Addr);
}
LocalList.set(key_position, dev_name);
Log.v("LOG", "modify success");
}
queryAllLocalList();
}
public String queryLocalList(int key_position)
{
String getResult = null;
String totalResult = null;
boolean isContains = false;
if((key_position >= 0) && (key_position <= 4))
{
try
{
getResult = (String)LocalList.get(key_position);
isContains = LocalListDetail.containsKey(getResult);
if (!isContains)
{
Log.v("LOG", "query LocalList and LocalListDetail not match");
totalResult = "LocalList(" + key_position + ") value = " + getResult;
return totalResult;
}
totalResult = "LocalList(" + key_position + ") device_name = " + getResult + "addr = " + LocalListDetail.get(getResult);
Log.v("LOG", totalResult);
}catch(Exception e){
//e.printStackTrace();
totalResult = "key_position index(" + key_position + ") fail";
Log.v("LOG", totalResult);
}
}
return totalResult;
}
public void queryAllLocalList()
{
for(int index = 0; index< 5; index++)
queryLocalList(index);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int s_index= -1;
//Add
s_index = addLocalList("kh's iphone", "aa:bb:cc:ee:dd:ff");
Log.v("LOG", "s_index = " + s_index);
s_index = addLocalList("kp's iphone", "aa:bb:cc:ee:dd:ff");
Log.v("LOG", "s_index = " + s_index);
s_index = addLocalList("scott's iphone", "bb:aa:ee:cc:dd:ff");
Log.v("LOG", "s_index = " + s_index);
s_index = addLocalList("Ivan's iphone", "ee:aa:ee:cc:dd:ff");
Log.v("LOG", "s_index = " + s_index);
s_index = addLocalList("CK's iphone", "dd:aa:cc:cc:dd:ff");
Log.v("LOG", "s_index = " + s_index);
//Delete
deleteLocalList(0);
deleteLocalList(2);
//Modify
modifyLocalList(0, "jj's i10Plus", "ff:ee:dd:cc:bb:aa");
//Query
queryLocalList(0);
queryLocalList(1);
//QueryAll
queryAllLocalList();
// a.add("011");
// a.add("111");
// a.add("211");
// a.add("311");
// a.add("411");
// Log.v("LOG", "a" + a.toString());
// //ccc = a.size();
// a.remove(2);
// String ccc = (String)a.get(2);
//
// int locate = a.indexOf("411");
// Log.v("LOG", "411 = " + locate);
// Log.v("LOG", "ccc = " + ccc);
//
// Log.v("LOG", "a" + a.toString());
// //System.out.println("ArrayList: " + a);
//
// // populate hash map
// newmap.put("111", "tutorials");
// newmap.put("222", "point");
// newmap.put("333", "is best");
//
// // get value of key 3
// String val=(String)newmap.get("111");
//
// // check the value
// System.out.println("Value for key 3 is: " + val);
}
}
Play Emperor Casino for FREE
回覆刪除Welcome to Emperor Casino ➤ No download, no registration or download necessary! ✓ Only 제왕 카지노 the best casino 샌즈카지노 games and 인카지노 bonuses ✓ Get your Sign Up Bonus now!