我正在开发一种工具,该工具可以扫描本地网络上的主机。这有效,然后将其添加到arraylist并显示。 (看起来像这样:https://gyazo.com/fe212f1705ea2be35f922625017addb7)
我想知道如何添加功能来保存此arraylist(名为:ipList)。我不确定要使用什么内容(GSON还是txt文件),然后将其保存到内部或外部存储,无论哪种方式对我来说都很好,但是理想情况下,我想从另一个活动中加载该文件(保存的列表)。
这是我的代码:
private Button btnScan;
private ListView listViewIp;
ArrayList<String> ipList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
btnScan = (Button)findViewById(R.id.scan);
listViewIp = (ListView)findViewById(R.id.listviewip);
ipList = new ArrayList<>();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
listViewIp.setAdapter(adapter);
final Gson gson = new Gson();
final String json = gson.toJson(ipList);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ScanIpTask().execute();
listViewIp.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
Intent intent = new Intent(Homepage.this, Details.class);
startActivity(intent);
break;
case 1:
// ipList.remove(1);
// Intent banaan = new Intent(Homepage.this, Register.class);
// startActivity(banaan);
Toast.makeText(getApplicationContext(), "Banan" + ipList.size(), Toast.LENGTH_SHORT).show();
case 3:
Toast.makeText(getApplicationContext(), json, Toast.LENGTH_SHORT).show();
break;
}
}
});
}
});}
String ip = "";
String subnet = "10.0.2.";
private String getSubnetAddress(int address)
{
String ipString = String.format(
"%d.%d.%d",
(address & 0xff),
(address >> 8 & 0xff),
(address >> 16 & 0xff));
return ipString;
}
class ScanIpTask extends AsyncTask<Void, String, Void>{
/*
Scan IP 192.168.1.100~192.168.1.110
you should try different timeout for your network/devices
*/
// static final String subnet = "10.0.2.";
static final int lower = 0;
static final int upper = 254;
static final int timeout = 50;
@Override
protected void onPreExecute() {
ipList.clear();
adapter.notifyDataSetInvalidated();
Toast.makeText(Homepage.this, "Scan IP...", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
for (int i = lower; i <= upper; i++) {
String host = subnet + i;
try {
InetAddress inetAddress = InetAddress.getByName(host);
if (inetAddress.isReachable(timeout)){
publishProgress(inetAddress.toString().split("/")[1]);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
ipList.add(values[0]);
adapter.notifyDataSetInvalidated();
Toast.makeText(Homepage.this, values[0], Toast.LENGTH_LONG).show();
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(Homepage.this, "Done", Toast.LENGTH_LONG).show();
}
}
所以我尝试将名为ipList的数组列表保存到GSON,但老实说我不知道该怎么做。
编辑
我添加了以下两个类来保存和加载数据,并为其添加了保存按钮。
private void saveData(){
SharedPreferences sharedpreferences = getSharedPreferences("Shared
Preferences", MODE_PRIVATE);
SharedPreferences.Editor editor =sharedpreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(ipList);
editor.putString("task list", json);
editor.apply();
Toast.makeText(Homepage.this, "Saved...", Toast.LENGTH_LONG).show();
}
private void loadData(){
Toast.makeText(Homepage.this, "Banaan...", Toast.LENGTH_LONG).show();
SharedPreferences sharedpreferences = getSharedPreferences("Shared Preferences", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedpreferences.getString("task list", null);
Type type = new TypeToken<ArrayList<String>>(){}.getType();
ipList = gson.fromJson(json, type);
if(ipList == null){
ipList = new ArrayList<>();
}
}
加载数据时出现错误 我认为这与以下行有关:Type type = new TypeToken>(){}。getType();
有什么提示吗?
答案 0 :(得分:0)
我想“另一个活动”在同一项目/应用程序中。
所以,为什么不使用SharedPreferences。您可以将所有主机(ips)保留在那里。 随处可见示例代码。希望这个小提示对您有所帮助。