gpt4 book ai didi

java - 在 Android Studio 中保存 Arraylist(Gson,或 Storage)

转载 作者:行者123 更新时间:2023-12-02 06:16:35 25 4
gpt4 key购买 nike

我正在开发一个工具来扫描本地网络上的主机。这是可行的,然后它将它添加到数组列表中并显示它。 (看起来像这样:https://gyazo.com/fe212f1705ea2be35f922625017addb7)

我想知道如何添加保存此数组列表(名为:ipList)的功能。我不知道该使用什么,GSON 或 txt 文件并将其保存到内部或外部存储,无论哪种方式对我来说都很好,但理想情况下我想从另一个 Activity 加载此文件(保存的列表)。

这是我的代码:

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

有什么建议吗?

最佳答案

我猜“另一个 Activity ”是在同一个项目/应用程序中。

那么,为什么不使用 SharedPreferences呢?您可以将所有主机(ip)保留在那里。示例代码随处可见。我希望这个小提示能对您有所帮助。

关于java - 在 Android Studio 中保存 Arraylist(Gson,或 Storage),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55869296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com