我从我的Api中获取我的Json对象/数组,并将其打印到我的视图中,foreach数组我在mainactivity中打印id和名称。
________________________
id name button <- if one clicks on the button another activity shall show the description of it
________________________
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private final static String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
// URL to get contacts JSON
private static String url = "http://192.168.178.58:8888/test";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
contactList = new ArrayList<>();
new GetContacts().execute();
lv = (ListView) findViewById(R.id.list);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
/**
* Async task class to get json by making HTTP call
*/
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null) {
try {
// Getting JSON Array node
JSONArray jsonArray = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String description = c.getString("description");
// tmp hash map for single contact
HashMap<String, String> data = new HashMap<>();
// adding each child node to HashMap key => value
data.put("id", id);
data.put("name", name);
data.put("description", description);
// adding contact to contact list
contactList.add(data);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
/**
* In onPostExecute() method the progress dialog is dismissed and the array list data is displayed in list view using an adapter.
* @param result
*/
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
*/
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item,
new String[]{"name", "description"},
new int[]{R.id.name, R.id.description});
lv.setAdapter(adapter);
}
}
}
这就是我的意图:
public void openDetails() { // button onclick name
Intent myIntent = new Intent(MainActivity.this, DetailActivity.class);
myIntent.putExtra("key", value);
MainActivity.this.startActivity(myIntent);
}
public class MyData {
private Object data;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
private static final MyData myData = new MyData();
public static MyData getInstance() {
return myData;
}
}
那么在第一个活动中:
MyData.getInstance().setData(objToShare);
第二个是:
objShared = MyData.getInstance().getData();
//do something
问题内容: 我正在尝试使用Apple的 SwiftUI 制作一个应用程序,我需要有两个按钮在同一行中显示两个不同的视图。 我使用 Xcode beta 7 和 MacOS Catalina beta 7 。我尝试添加一个表示视图的视图,但是无法单击它,而当我尝试在外部进行简单单击并单击该视图时,该视图没有出现。我也尝试过添加一个内部,但是它也不起作用。单击时也无法添加,视图仍然不会出现 我希望该视
我有一个Android应用程序内置在Android studio,在这个应用程序上,我正在使用一个演练活动。 如何设置此活动的方式,当一个按钮被点击,该页将不会再次显示。
在我的项目中,需求是创建一个回收器视图->卡片视图在活动a中,当信息被填满并在活动B中按下按钮时。
我是新的Android和建立一个小应用程序,从相机拍照,并保存到画廊。 下面是捕获图像的函数。 这是我们的主要活动。xml 当图像被捕获时,我想在另一个活动(页面)上显示图像,而不是在具有捕获图像按钮的同一活动上。如何做到这一点。 提前谢谢