// MainActivity.java
公共类MainActivity扩展AppCompatActivity {
private static final String JSON_URL =" http://kedaiagan.000webhostapp.com/kedaiagan/kedaiagan/menus.php&#34 ;;
列表menusList;
ListView listView;
私人按钮btnKirimPesan;
EditText nomeja;
AlertDialog.Builder构建器;
JSONArray jsonArray = new JSONArray();
//initializing objects
private void inisialisasi(){
listView = (ListView) findViewById(R.id.listview);
menusList = new ArrayList<>();
btnKirimPesan= (Button) findViewById(R.id.btnKirimPesan);
nomeja = (EditText) findViewById(R.id.input_meja);
builder = new AlertDialog.Builder(MainActivity.this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inisialisasi();
//this method will fetch and parse the data
loadMenuList();
btnKirimPesan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < listView.getCount(); i++) {
View v = getViewByPosition(i, listView);
String id_menu = ((TextView) v.findViewById(R.id.textViewIdMenu)).getText().toString();
String ket_menu = ((EditText) v.findViewById(R.id.textKeterangan)).getText().toString();
String qty_menu = ((EditText) v.findViewById(R.id.textQuantity)).getText().toString();
JSONObject pesanan = new JSONObject();
try {
if(!qty_menu.equals("")) {
if (Integer.valueOf(qty_menu) >= 1) {
pesanan.put("id_menu", id_menu);
pesanan.put("keterangan", ket_menu);
pesanan.put("jumlah_porsi", qty_menu);
jsonArray.put(pesanan);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String url = "http://kedaiagan.000webhostapp.com/kedaiagan/kedaiagan/insertpesanan.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
builder.setTitle("Pesanan Terkirim");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
nomeja.setText("");
Intent intent = new Intent(MainActivity.this, HalUtama.class);
startActivity(intent);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this, volleyError.toString(), Toast.LENGTH_LONG).show();
volleyError.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("nomeja", nomeja.getText().toString());
params.put("pesanan", jsonArray.toString());
return params;
}
};
Process.getInstance(MainActivity.this).addTorequestque(stringRequest);
}
public View getViewByPosition(int position, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (position < firstListItemPosition || position > lastListItemPosition ) {
return listView.getAdapter().getView(position, listView.getChildAt(position), listView);
} else {
final int childIndex = position - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
});
}
public void getTotalHeightofListView() {
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
int height = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
height = mView.getMeasuredHeight();
totalHeight += mView.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1))
+ height;
Log.i("Height", "" + totalHeight);
listView.setLayoutParams(params);
listView.requestLayout();
}
private void loadMenuList() {
//getting the progressbar
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
//making the progressbar visible
progressBar.setVisibility(View.VISIBLE);
//creating a string request to send request to the url
StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//hiding the progressbar after completion
progressBar.setVisibility(View.INVISIBLE);
try {
//getting the whole json object from the response
JSONObject obj = new JSONObject(response);
//we have the array named hero inside the object
//so here we are getting that json array
JSONArray menuArray = obj.getJSONArray("menus");
//now looping through all the elements of the json array
for (int i = 0; i < menuArray.length(); i++) {
//getting the json object of the particular index inside the array
JSONObject menuObject = menuArray.getJSONObject(i);
//creating a hero object and giving them the values from json object
Menus menu = new Menus(menuObject.getString("nama_menu"), menuObject.getString("id_menu"));
//adding the hero to herolist
menusList.add(menu);
}
//creating custom adapter object
final MyListAdapter adapter = new MyListAdapter(menusList, getApplicationContext());
//adding the adapter to listview
listView.setAdapter(adapter);
getTotalHeightofListView();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//displaying the error in toast if occurrs
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("error", error.getMessage());
}
});
//creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//adding the string request to request queue
requestQueue.add(stringRequest);
}
}
// MyListAdapter.java
public class MyListAdapter extends ArrayAdapter<Menus> {
//the hero list that will be displayed
List<Menus> menusList;
//the context object
Context mCtx;
//here we are getting the herolist and context
//so while creating the object of this adapter class we need to give herolist and context
public MyListAdapter(List<Menus> menusList, Context mCtx) {
super(mCtx, R.layout.my_custom_list, menusList);
this.mCtx = mCtx;
this.menusList = menusList;
}
//this method will return the list item
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//getting the layoutinflater
LayoutInflater inflater = LayoutInflater.from(mCtx);
//creating a view with our xml layout
View listViewItem = inflater.inflate(R.layout.my_custom_list, null, true);
//getting text views
TextView textViewName = listViewItem.findViewById(R.id.textViewName);
TextView textViewIdMenu = listViewItem.findViewById(R.id.textViewIdMenu);
//Getting the hero for the specified position
Menus hero = menusList.get(position);
//setting hero values to textviews
textViewName.setText(hero.getNama_menu());
textViewIdMenu.setText(hero.getId_menu());
//returning the listitem
return listViewItem;
}
}
// Menus.java
public class Menus {
String nama_menu;
String id_menu;
public Menus(String nama_menu, String id_menu) {
this.nama_menu = nama_menu;
this.id_menu = id_menu;
}
public String getNama_menu() {
return nama_menu;
}
public String getId_menu() {
return id_menu;
}
}
1. List item
答案 0 :(得分:0)
简单的方法是使用GSON将JSON字符串解析为POJO,然后从那里获得POJO类型的列表,然后您可以在列表中应用所需的过滤器以生成所需的列表。
import java.util.ArrayList;
public class ListHandler{
private ArrayList<POJO_Name> metrics;
public ArrayList<POJO_Name> getMetrics() {
return metrics;
}
public void setMetrics(ArrayList<POJO_Name> metrics) {
this.metrics = metrics;
}
}
这将生成一个列表实例,您可以使用GSON解析JSON。并且用于解析过程
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
ListHandler handler= gson.fromJson(json_string, POJOName.class);
确保您必须序列化POJO。如果你想创造没有痛苦的POJO,我建议你使用JSON to POJO。
现在完成所有操作只需创建一个方法,在基本列表上应用条件后返回一个新列表,这样就不会弄乱基本列表。