ListVew从Android中的MySql数据库重复相同的行

时间:2017-08-07 05:41:08

标签: android listview

我有一个ListVew,我必须在数据库中显示订单列表。我在Mysql数据库中有多个订单,但在ListVew中只有一个订单不断重复。有关这是赞赏的。 < / p>

这是我的适配器类

public class OrderApprovalAdapter extends ArrayAdapter
{
List list1 = new ArrayList();
Context context;

public OrderApprovalAdapter(@NonNull Context context, @NonNull int Resource)
{
    super(context, Resource);
    this.context = context;
}
@Override
public void add(@Nullable Object object)
{
    super.add(object);
    list1.add(object);
}

@Override
public int getCount()
{
    return list1.size();
}

@Override
public Object getItem(int position)
{
    return list1.get(position);
}



@Override
public long getItemId(int position)
{
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    final OrderApprovalAdapter.ContactHolder contactHolder;

    View row;
    row = convertView;

    if (row == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.order_approval_format, parent, false);
        contactHolder = new ContactHolder();
        contactHolder.date = (TextView) row.findViewById(R.id.invoice_date);
        contactHolder.orderid = (TextView) row.findViewById(R.id.invoice_no);
        contactHolder.shopname = (TextView) row.findViewById(R.id.shop_name);
        contactHolder.ownername = (TextView) row.findViewById(R.id.owner_name);
        contactHolder.mobile=(TextView) row.findViewById(R.id.mobile_noo);
        contactHolder.location=(TextView)row.findViewById(R.id.location);
        contactHolder.itemscount=(TextView)row.findViewById(R.id.items);
        contactHolder.amount=(TextView)row.findViewById(R.id.total);
        contactHolder.Approval=(Button)row.findViewById(R.id.Approve_btn);
        contactHolder.Decline=(Button)row.findViewById(R.id.decline_btn);



        row.setTag(contactHolder);
    } else
    {
        contactHolder = (ContactHolder) row.getTag();
    }


    final OrderApprovalDetails contacts = (OrderApprovalDetails) this.getItem(position);
    contactHolder.date.setText(contacts.getDate());
    contactHolder.orderid.setText(contacts.getOrderid());
    contactHolder.shopname.setText(contacts.getShopname());
    contactHolder.ownername.setText(contacts.getOwnername());
    contactHolder.mobile.setText(contacts.getMobile());
    contactHolder.location.setText(contacts.getLocation());
    contactHolder.itemscount.setText(contacts.getItemscount());
    contactHolder.amount.setText(contacts.getAmount());
    contactHolder.Approval.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Toast.makeText(context, "Approved", Toast.LENGTH_SHORT).show();

        }
    });
    contactHolder.Decline.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Toast.makeText(context, "Decline", Toast.LENGTH_SHORT).show();
        }
    });

    return row;
}
static class ContactHolder
{
    TextView date,orderid,shopname,ownername,mobile,location,itemscount,amount;
    Button Approval,Decline;

}
}

从数据库中重新获取数据

 class OrdersListBackgroundTask extends AsyncTask<Void,Void,String>
{

    @Override
    protected void onPreExecute()
    {
        OrdersList_url="http://10.0.2.2/accounts/OrderForm2.php";
    }

    @Override
    protected String doInBackground(Void... params)
    {

        try {
            URL url=new URL(OrdersList_url);
            HttpURLConnection httpURLConnection=
      (HttpURLConnection)url.openConnection();
            InputStream is= httpURLConnection.getInputStream();
            InputStreamReader isr=new InputStreamReader(is);
            BufferedReader br=new BufferedReader(isr);
            StringBuilder sb=new StringBuilder();

            while ((JSON_STRING4=br.readLine())!=null)
            {
                sb.append(JSON_STRING4+ "");
            }

            br.close();
            is.close();
            httpURLConnection.disconnect();

            return sb.toString().trim();
        } catch (MalformedURLException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    protected void onPostExecute(String result)
    {
        //TextView tv=(TextView)findViewById(R.id.tv);
        //tv.setText(result);
        json_string4=result;
    }
}

通过意图发送到下一页

    public void Orders_List(View v)//Button click
    {
    if (json_string4==null)
    {
        Toast.makeText(this, "Get Orders First", Toast.LENGTH_SHORT).show();
    }else
    {
        Intent i1=new Intent(this,OrdersList.class);
        i1.putExtra("json_data4",json_string4);
        startActivity(i1);
    }
    }

MainActivity中的用法

    json_string4=getIntent().getExtras().getString("json_data4");
    listView_Orders=(ListView)findViewById(R.id.listViewOrderlist);
    listView_Orders.setItemsCanFocus(true);
    search_filter=(EditText)findViewById(R.id.search_et);
    orderApprovalAdapter=new 
    OrderApprovalAdapter(this,R.layout.order_approval_format);
    listView_Orders.setAdapter(orderApprovalAdapter);
    //listView_Orders.setTextFilterEnabled(true);

    try
    {
        jsonObject=new JSONObject(json_string4);
        jsonArray=jsonObject.getJSONArray("orders");
        int count=0;

       for (int i=0;i<jsonArray.length();i++)
       {
           JSONObject jo=jsonArray.getJSONObject(i);
            date=jo.getString("date");
            orderid=jo.getString("orderid");
            shopname=jo.getString("shopname");
            ownername=jo.getString("ownername");
            mobile=jo.getString("mobile");
            location=jo.getString("location");
            items=jo.getString("items_count");
            amount=jo.getString("amount");

           OrderApprovalDetails orderApprovalDetails=new OrderApprovalDetails(date,orderid,shopname,ownername,mobile,location,items,amount,Approve,Decline);
           orderApprovalAdapter.add(orderApprovalDetails);
       }
    } catch (JSONException e)
    {
        e.printStackTrace();
    }

}

OrderApprovalDetails Class

public class OrderApprovalDetails
{
public static String 
date,orderid,shopname,ownername,mobile,location,itemscount,amount;
public static Button Approval,Decline;

public OrderApprovalDetails(String date,String orderid,String shopname,String ownername,String mobile,String location,String itemscount,String amount,Button Approval,Button Decline)
{
    this.setDate(date);
    this.setOrderid(orderid);
    this.setShopname(shopname);
    this.setOwnername(ownername);
    this.setMobile(mobile);
    this.setLocation(location);
    this.setItemscount(itemscount);
    this.setAmount(amount);
    this.setApproval(Approval);
    this.setDecline(Decline);
}

public String getDate()
{
    return date;
}

public static Button getApproval() {
    return Approval;
}

public static void setApproval(Button approval) {
    Approval = approval;
}

public static Button getDecline() {
    return Decline;
}

public static void setDecline(Button decline) {
    Decline = decline;
}

public void setDate(String date) {
    this.date = date;
}

public String getOrderid() {
    return orderid;
}

public void setOrderid(String orderid) {
    this.orderid = orderid;
}

public String getShopname() {
    return shopname;
}

public void setShopname(String shopname) {
    this.shopname = shopname;
}

public String getOwnername() {
    return ownername;
}

public void setOwnername(String ownername) {
    this.ownername = ownername;
}

public String getMobile() {
    return mobile;
}

public void setMobile(String mobile) {
    this.mobile = mobile;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getItemscount() {
    return itemscount;
}

public void setItemscount(String itemscount) {
    this.itemscount = itemscount;
}

public String getAmount() {
    return amount;
}

public void setAmount(String amount) {
    this.amount = amount;
}

}

不知道我在哪里犯了错误,请帮助我找到它,先谢谢。

1 个答案:

答案 0 :(得分:1)

你的班级成员不应该像你在这里那样被宣布为静态 -

public class OrderApprovalDetails
{
    public static String date,orderid,shopname,ownername,mobile,location,itemscount,amount;

将此更改为

public class OrderApprovalDetails
    {
    public String date,orderid,shopname,ownername,mobile,location,itemscount,amount;