Java 在列表视图中重复的所有项目
我目前正在为一家杂货店开发一个送货员应用程序。因此,在一个屏幕中,我需要显示所有的细节——客户姓名、地址、地标和日期以及杂货清单。我正在使用列表视图显示所有需要交付的项目。我想在同一屏幕上显示客户详细信息。但我想,由于listview,细节不断重复。 这是我正在使用的xml文件-Java 在列表视图中重复的所有项目,java,android,xml,android-layout,listview,Java,Android,Xml,Android Layout,Listview,我目前正在为一家杂货店开发一个送货员应用程序。因此,在一个屏幕中,我需要显示所有的细节——客户姓名、地址、地标和日期以及杂货清单。我正在使用列表视图显示所有需要交付的项目。我想在同一屏幕上显示客户详细信息。但我想,由于listview,细节不断重复。 这是我正在使用的xml文件- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="7dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/cust_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="@color/bb_darkBackgroundColor"
android:textSize="18sp"
android:visibility="visible" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/landmark"
android:layout_marginTop="13dp"
android:text="Delivery Date"
android:textColor="@color/bb_darkBackgroundColor"
android:textSize="18sp"
android:visibility="visible" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/relativeLayout"
android:layout_marginTop="37dp"
android:layout_toLeftOf="@+id/tv_quantity"
android:layout_toStartOf="@+id/tv_quantity"
android:text="Address"
android:textColor="@color/bb_darkBackgroundColor"
android:textSize="18sp"
android:visibility="visible" />
<TextView
android:id="@+id/landmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/relativeLayout"
android:layout_alignStart="@+id/relativeLayout"
android:layout_alignTop="@+id/relativeLayout"
android:layout_marginTop="72dp"
android:text="TextView"
android:textColor="@color/bb_darkBackgroundColor"
android:textSize="18sp"
android:visibility="visible" />
</RelativeLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:dividerHeight="1dp"
android:layout_above="@+id/r1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"></ListView>
<RelativeLayout
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="@+id/btn_order_collected"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:background="@drawable/blue_button_background"
android:text="Order Collected "
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="18sp"
android:layout_marginTop="31dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp" />
<Button
android:id="@+id/btn_go_to_map"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_order_deliver"
android:layout_alignStart="@+id/btn_order_deliver"
android:layout_below="@+id/btn_order_deliver"
android:layout_marginTop="28dp"
android:background="@drawable/white_button_background"
android:text="Go To Map"
android:textAllCaps="false"
android:textColor="@color/theme_color"
android:textSize="18sp" />
<Button
android:id="@+id/call_customer"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_go_to_map"
android:layout_alignBottom="@+id/btn_go_to_map"
android:layout_alignLeft="@+id/btn_order_collected"
android:layout_alignStart="@+id/btn_order_collected"
android:background="@drawable/white_button_background"
android:text="Call Customer"
android:textAllCaps="false"
android:textColor="@color/theme_color"
android:textSize="18sp" />
<Button
android:id="@+id/btn_order_deliver"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/btn_order_collected"
android:layout_marginEnd="38dp"
android:layout_marginRight="38dp"
android:background="@drawable/blue_button_background"
android:text="Delivered"
android:textColor="@color/colorWhite"
android:textSize="18sp" />
</RelativeLayout>
</RelativeLayout>
这是我的订单_product.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:padding="10dp">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="21dp"
android:layout_marginStart="21dp"><![CDATA[
android:id="@+id/relativeLayout"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true">
<TextView
android:id="@+id/tv_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_no_of_item"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/tv_no_of_item"
android:text="Qty: 180 ml"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Product Name"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_no_of_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_product_name"
android:layout_marginTop="10dp"
android:text="No. of bottle: 5"
android:textColor="@android:color/black"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
这是我的java文件:-
public class ItemTwoFragment extends Fragment implements
AsyncResponseCurrentOrder {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private Button btn_order_collected, btn_go_to_map, btn_call_customer,
btn_order_deliver;
private OnFragmentInteractionListener mListener;
private ListView listView;
currentOrder cOrder;
public ItemTwoFragment(){}
public static ItemTwoFragment newInstance(String param1, String param2) {
ItemTwoFragment fragment = new ItemTwoFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.current_orders, container, false);
btn_order_collected = (Button)
view.findViewById(R.id.btn_order_collected);
btn_go_to_map = (Button) view.findViewById(R.id.btn_go_to_map);
btn_call_customer = (Button) view.findViewById(R.id.call_customer);
listView = (ListView) view.findViewById(R.id.listview);
btn_order_deliver = (Button) view.findViewById(R.id.btn_order_deliver);
btn_order_deliver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedpreferences =
getActivity().getSharedPreferences("MY" , Context.MODE_PRIVATE);
String driver_token =
sharedpreferences.getString("driver_token", "");
String order_no = sharedpreferences.getString("current_order_no"
, "");
new orderDeliver(getActivity(), driver_token ,
order_no).execute();
}
});
btn_order_collected.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedpreferences =
getActivity().getSharedPreferences("MY" , Context.MODE_PRIVATE);
String driver_token =
sharedpreferences.getString("driver_token", "");
String order_no = sharedpreferences.getString("current_order_no"
, "");
new orderPickup(getActivity(), driver_token ,
order_no).execute();
// Toast.makeText(getContext(),"Order
Collected",Toast.LENGTH_SHORT).show();
}
});
/* btn_go_to_map.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.tabLayout.setScrollPosition(1, 0f, true);
MainActivity.viewPager.setCurrentItem(1);
}
});*/
btn_call_customer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedpreferences =
getActivity().getSharedPreferences("Customer" , Context.MODE_PRIVATE);
String customer_phone =
sharedpreferences.getString("customer_phone", "");
Preferences preferences= new Preferences(getContext());
String uri = "tel:" + customer_phone;
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse(uri));
startActivity(callIntent);
}
});
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser){
SharedPreferences sharedpreferences =
getActivity().getSharedPreferences("MY" , Context.MODE_PRIVATE);
String driver_token = sharedpreferences.getString("driver_token",
"");
String order_no = sharedpreferences.getString("current_order_no" ,
"");
cOrder = new currentOrder(getActivity() ,driver_token , order_no);
cOrder.delegate= this;
cOrder.execute();
}
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
/* throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");*/
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void processFinish1(List<Order> cOrder) {
OrderListAdapter orderListAdapter = new OrderListAdapter(getContext(),
cOrder);
listView.setAdapter(orderListAdapter);
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
private class orderPickup extends AsyncTask<String, Void, String> {
Context activity ;
String device_token , order_no;
String status = "";
public orderPickup(Context activity, String device_token, String
order_no) {
this.activity = activity;
this.device_token = device_token;
this.order_no = order_no;
}
@Override
protected String doInBackground(String... params) {
try {
String result= WebService.orderPickup(device_token , order_no,
activity);
JSONObject jsonObject = new JSONObject(result);
status = jsonObject.optString("msg");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return status;
}
@Override
protected void onPostExecute(String status) {
super.onPostExecute(status);
// MainActivity.tabLayout.setScrollPosition(1, 0f, true);
// MainActivity.viewPager.setCurrentItem(1);
Toast.makeText(activity , status , Toast.LENGTH_LONG).show();
}
}
//order deliver update
private class orderDeliver extends AsyncTask<String, Void, String> {
Context activity ;
String device_token , order_no;
String status = "";
public orderDeliver(Context activity, String device_token, String
order_no) {
this.activity = activity;
this.device_token = device_token;
this.order_no = order_no;
}
@Override
protected String doInBackground(String... params) {
try {
String result= WebService.orderDeliver(device_token , order_no,
activity);
JSONObject jsonObject = new JSONObject(result);
status = jsonObject.optString("msg");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return status;
}
@Override
protected void onPostExecute(String status) {
super.onPostExecute(status);
// MainActivity.tabLayout.setScrollPosition(1, 0f, true);
// MainActivity.viewPager.setCurrentItem(1);
Toast.makeText(activity , status , Toast.LENGTH_LONG).show();
}
}
}
class currentOrder extends AsyncTask<String, Void, List<Order>> {
public AsyncResponseCurrentOrder delegate = null;
Activity activity ;
String device_token, order_no;
ProgressDialog progressDialog;
public currentOrder(Activity activity, String device_token, String
order_no) {
this.activity = activity;
this.device_token = device_token;
this.order_no=order_no;
}
@Override
protected void onPreExecute() {
progressDialog= ProgressDialog.show(activity, "Loading. . .","Please Wait. . .", true);
super.onPreExecute();
}
@Override
protected List<Order> doInBackground(String... params) {
List<Order> orderList = new ArrayList<>();
try {
String result= WebService.orderDetails(device_token,order_no, activity);
JSONObject jsonObject = new JSONObject(result);
Log.d("Json Array", "doInBackground: "+jsonObject);
String status = jsonObject.optString("error");
if(status.equals("0")){
JSONArray jsonarray = jsonObject.getJSONArray("data");
for(int i=0;i<jsonarray.length(); i++){
//jObj.getString("address"), jObj.getString("landmark"),jObj.getString("date"),
JSONObject jObj = jsonarray.getJSONObject(i);
Order order = new Order(jObj.getString("customer_name"),jObj.getString("delivery_address1"),jObj.getString("delivery_date"), jObj.getString("delivery_landmark"), jObj.getString("product_name"), jObj.getString("variant_name") ,jObj.getInt("qty"));
SharedPreferences sharedpreferences = activity.getSharedPreferences("Customer" , Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
// editor.putString("customer_name",jObj.getString("customer_name"));
// editor.putString("date",jObj.getString("date"));
//editor.putString("landmark",jObj.getString("landmark"));
//editor.putString("address",jObj.getString("address"));
editor.putString("delivery_latitude",jObj.getString("delivery_latitude"));
editor.putString("delivery_longitude",jObj.getString("delivery_longitude"));
editor.putString("delivery_address1",jObj.getString("delivery_address1"));
editor.putString("delivery_address2",jObj.getString("delivery_address2"));
editor.putString("customer_name",jObj.getString("customer_name"));
editor.putString("customer_phone",jObj.getString("customer_phone"));
editor.commit();
orderList.add(order);
}
}
else {
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return orderList;
}
protected void onPostExecute(List<Order> orderList) {
delegate.processFinish1(orderList);
progressDialog.dismiss();
}
}
public类ItemTwoFragment扩展了片段实现
AsyncResponseCurrentOrder{
私人回收视图mRecyclerView;
专用回收器查看适配器mAdapter;
private RecyclerView.LayoutManager MLLayoutManager;
私有静态最终字符串ARG_PARAM1=“PARAM1”;
私有静态最终字符串ARG_PARAM2=“PARAM2”;
私人按钮btn\U订单收集,btn\U前往地图,btn\U呼叫客户,
btn_订单_交付;
私有OnFragmentInteractionListener mListener;
私有列表视图列表视图;
电流顺序记录器;
public ItemTwoFragment(){}
公共静态ItemTwoFragment newInstance(字符串param1,字符串param2){
ItemTwoFragment=新的ItemTwoFragment();
Bundle args=新Bundle();
args.putString(ARG_PARAM1,PARAM1);
args.putString(ARG_PARAM2,PARAM2);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.current_orders,container,false);
btn_订单_已收集=(按钮)
view.findViewById(R.id.btn\u订单\u已收集);
btn_go_to_map=(按钮)view.findviewbyd(R.id.btn_go_to_map);
btn_call_customer=(按钮)view.findviewbyd(R.id.call_customer);
listView=(listView)view.findViewById(R.id.listView);
btn\u order\u deliver=(按钮)view.findViewById(R.id.btn\u order\u deliver);
btn_order_deliver.setOnClickListener(新视图.OnClickListener()的缩写){
@凌驾
公共void onClick(视图v){
SharedReferences SharedReferences=
getActivity().getSharedReferences(“我的”,Context.MODE_PRIVATE);
字符串驱动程序\u令牌=
getString(“驱动程序令牌”,“驱动程序令牌”);
字符串顺序号=SharedReferences.getString(“当前顺序号”
, "");
新orderDeliver(getActivity(),驱动程序\u令牌,
订单号)。执行();
}
});
btn_order_collected.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
SharedReferences SharedReferences=
getActivity().getSharedReferences(“我的”,Context.MODE_PRIVATE);
字符串驱动程序\u令牌=
getString(“驱动程序令牌”,“驱动程序令牌”);
字符串顺序号=SharedReferences.getString(“当前顺序号”
, "");
新订单分拣(getActivity(),驱动程序\u令牌,
订单号)。执行();
//Toast.makeText(getContext(),“Order
收集“,吐司。长度(短)。show();
}
});
/*btn_go_to_map.setOnClickListener(newview.OnClickListener()){
@凌驾
公共void onClick(视图){
MainActivity.TableLayout.setScrollPosition(1,0f,真);
MainActivity.viewPager.setCurrentItem(1);
}
});*/
btn_call_customer.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
SharedReferences SharedReferences=
getActivity().getSharedReferences(“客户”,Context.MODE_PRIVATE);
字符串客户电话=
getString(“customer_phone”,下称“);
首选项首选项=新的首选项(getContext());
String uri=“电话:”+客户电话;
意向callIntent=新意向(意向.行动拨号);
setData(Uri.parse(Uri));
星触觉;
}
});
返回视图;
}
@凌驾
public void setUserVisibleHint(布尔值isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
如果(isVisibleToUser){
SharedReferences SharedReferences=
getActivity().getSharedReferences(“我的”,Context.MODE_PRIVATE);
String driver\u token=SharedReferences.getString(“driver\u token”,
"");
字符串顺序号=SharedReferences.getString(“当前顺序号”,
"");
cOrder=新的currentOrder(getActivity(),驱动程序令牌,订单号);
cOrder.delegate=this;
cOrder.execute();
}
}
public void onButtonPressed(Uri){
if(mListener!=null){
onFragmentInteraction(uri);
}
}
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
if(OnFragmentInteractionListener的上下文实例){
mListener=(OnFragmentInteractionListener)上下文;
}否则{
/*抛出新的RuntimeException(context.toString()
+“必须实现OnFragmentInteractionListener”)*/
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
@凌驾
公共作废处理完成1(列表记录器){
OrderListAdapter OrderListAdapter=新的OrderListAdapter(getContext(),
录像机);
setAdapter(orderListAdapter);
}
FragmentInteractionListener上的公共接口{
//TODO:更新参数类型和名称
void onFragmentInteraction(Uri);
}
私有类OrderTask{
语境活动;
字符串设备\u令牌,订单号;
字符串状态=”;
公共订单拾取(上下文活动、字符串设备\令牌、字符串
订单编号({
这个。活动=活动;
this.device\u token=设备\u token;
this.order\u no=订单号;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
字符串结果=WebService.OrderPick(设备令牌、订单号、,
活动);
JS
public class OrderListAdapter extends BaseAdapter {
Context context;
List<Order> orderList = null;
LayoutInflater layoutInflater;
boolean tickOrder = false, cancelOrder = false;
public OrderListAdapter(Context context, List<Order> orderList) {
this.context = context;
this.orderList = orderList;
layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return orderList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return orderList.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
static class ViewHolder {
TextView tvProductName,tvNoOfItem,tvQuantity, tv_cust_name, tv_address,
tv_landmark, tv_date;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
if (view == null) {
view = layoutInflater.inflate(R.layout.order_product, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.tv_cust_name= (TextView)view.findViewById(R.id.cust_name);
viewHolder.tv_address = (TextView) view.findViewById(R.id.address);
viewHolder.tv_landmark= (TextView)view.findViewById(R.id.landmark);
viewHolder.tv_date= (TextView)view.findViewById(R.id.date);
viewHolder.tvProductName = (TextView) view.findViewById(R.id.tv_product_name);
viewHolder.tvNoOfItem = (TextView) view.findViewById(R.id.tv_no_of_item);
viewHolder.tvQuantity = (TextView) view.findViewById(R.id.tv_quantity);
//viewHolder.cancelOrder = (ImageButton) view.findViewById(R.id.btn_cross);
//viewHolder.tickOrder = (ImageButton) view.findViewById(R.id.btn_tick);
view.setTag(viewHolder);
}
Order order = (Order) getItem(position);
final ViewHolder holder = (ViewHolder) view.getTag();
holder.tv_cust_name.setText(order.getCustomer_name());
holder.tv_address.setText(order.getAddress());
holder.tv_landmark.setText(order.getLandmark());
holder.tv_date.setText(order.getDate());
holder.tvProductName.setText(order.getProductName());
holder.tvNoOfItem.setText("Qty: " + order.getQuantity());
holder.tvQuantity.setText("Size: " + order.getVariantName());
/* holder.cancelOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!cancelOrder){
holder.cancelOrder.setBackgroundResource(R.drawable.cancel_order);
holder.tickOrder.setBackgroundResource(R.drawable.tick_inactive);
cancelOrder = true;
tickOrder = false;
}else {
holder.cancelOrder.setBackgroundResource(R.drawable.cross_inactive);
cancelOrder = false;
tickOrder = true;
}
}
});*/
/* holder.tickOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!tickOrder){
holder.tickOrder.setBackgroundResource(R.drawable.order_active);
holder.cancelOrder.setBackgroundResource(R.drawable.cross_inactive);
tickOrder = true;
cancelOrder = false;
}else {
holder.tickOrder.setBackgroundResource(R.drawable.tick_inactive);
tickOrder = false;
cancelOrder = true;
}
}
});*/
return view;
}
}