所有项目在列表视图中重复

时间:2017-07-13 09:15:19

标签: java android xml android-layout listview

我目前正在为杂货店开发送货员申请表。所以在一个屏幕上,我需要显示所有细节 - 客户名称,地址,地标和日期以及购物清单。我使用列表视图来显示需要传递的所有项目。我想在同一个屏幕上显示客户详细信息。但我想,由于列表视图,细节不断重复。 这是使用 -

的xml文件
<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>

这是我的order_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 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;
}
}

提前致谢!

0 个答案:

没有答案