从Listview Imlemented使用Baseadapter

时间:2015-09-25 12:37:03

标签: android baseadapter

我有一个使用此代码的适配器:

        public class LoadOrders_adapter extends BaseAdapter {

private JSONArray data;
private Context context;

public LoadOrders_adapter(JSONArray data, Context context) {
    this.data = data;
    this.context = context;
}

@Override
public int getCount() {

        return data.length();

}

@Override
public Object getItem(int position) {
    try {
        return data.get(position);
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent){

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.loading_orderlist, parent, false);

    ImageView product_images=(ImageView) row.findViewById(R.id.product_images);
    TextView Total_quantity = (TextView) row.findViewById(R.id.Total_quantity);
    TextView order_status=(TextView) row.findViewById(R.id.order_status);
    TextView order_date = (TextView) row.findViewById(R.id.order_date);
    TextView order_id = (TextView) row.findViewById(R.id.order_id);
    TextView product_Name=(TextView) row.findViewById(R.id.product_Name);

    try {
        JSONObject temp = data.getJSONObject(position);
        Picasso.with(context).load(WebConnection.getInstance().resource_url(temp.getString("imgUrl"))).into(product_images);
        Total_quantity.setText(temp.getString("Quantity"));
        order_date.setText(temp.getString("Date_Sub"));
        order_id.setText("#"+temp.getString("Order_ID"));
        order_status.setText(temp.getString("Status"));
        product_Name.setText(temp.getString("Name"));

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return row;

}

}

一个叫做的课程:

    public class LoadOrders extends ActionBarActivity implements Top_fragment.Top_fragmentListener {

private JSONArray data = null;
private JSONArray OrderDetails2 = null;
private ListView Normal_Orders_list;
private String previous_activity = "info.sliit.mystyle.Home";
private String title_name = "Your Normal Orders";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_promotional_wear);
    Normal_Orders_list = (ListView)findViewById(R.id.Normal_Orders_list);
    new BackgroundProcess().execute();
}

class BackgroundProcess extends AsyncTask<Void,Void,Void> {

    ProgressDialog progressDialog = new ProgressDialog(LoadOrders.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog.setMessage("Loading data...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        data = WebConnection.getInstance().get_request("Loading_order_rest/orderloading/user_id/12","json");
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        progressDialog.dismiss();
        BaseAdapter baseAdapter1 = new LoadOrders_adapter(data,LoadOrders.this);
        Normal_Orders_list.setAdapter(baseAdapter1);
        //Normal_Orders_list.setOnItemClickListener();

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_customized_orders,menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
@Override
public String get_previous_activity() {
    return previous_activity;
}

@Override
public String getTitle_name() {
    return title_name;
}

}

我有一个名为Activity_loading_orders的活动,它有一个名为Normal_Orders_list的列表视图,我有另一个名为loading_orderlist的布局,它有一个带有一些Textviews和一个按钮的RelativeLayout。正如您在代码中看到的那样,布局loading_orderlist作为列表项加载到Normal_Orders_list活动中。我想要做的是在单击按钮时删除列表项,请参阅图像http://i.imgur.com/TYAqd0t.jpg

我的杰森从邮差中取出

    <?xml version="1.0" encoding="utf-8"?>
<xml><data><datum><Order_ID>8</Order_ID><Quantity>52</Quantity><Comment>fs</Comment><Date_Sub>2015-08-27</Date_Sub><Date_Del/><Product_ID/><Customer_ID>12</Customer_ID><Status>Accepted</Status><View>Unread</View><Name>T-shirt</Name><imgUrl>\assets\images\projectpics\normalt\Edited_front.jpg</imgUrl></datum><datum><Order_ID>10</Order_ID><Quantity>45</Quantity><Comment>sf</Comment><Date_Sub>2015-08-27</Date_Sub><Date_Del/><Product_ID/><Customer_ID>12</Customer_ID><Status>Accepted</Status><View>Unread</View><Name>T-shirt</Name><imgUrl>\assets\images\projectpics\normalt\Edited_front.jpg</imgUrl></datum></data></xml>

2 个答案:

答案 0 :(得分:0)

考虑在getView()之外移动JSON解析。你想要做的是:

  1. 拥有List <ParsedJsonObject> JsonArray(来自解析JSON)而不是mListViewAdapter.notifyDataSetChanged();
  2. 开启&#39;取消&#39;单击:从列表中删除项目
  3. 从列表中删除项目后,通知适配器其数据集已更改:ArrayAdapter
  4. 请注意:我担心您必须更改为自定义BaseAdapter而不是JSONArray。原因是您使用List<ParsedJsonObject>作为适配器的数据集。自己开始搞乱JSON并不是一个好主意,但是您必须以这种或那种方式从数据集中删除该项目。我建议您查看gsonGenson等库。

    这些库可以为您将JSON反序列化为Java对象。那么你最终会做什么:

    1. 获取JSON数据
    2. 将JSON数据反序列化为Java对象,将这些对象放入ArrayAdapter<ParsedJsonObject>
    3. 制作新的ParsedJsonObject
    4. ArrayAdapter列表作为您刚刚创建的getView()的数据集传递。
    5. getView()不必改变很多,只需改变数据的来源。 List<ParsedJsonObject>可让您知道自己所处的位置,并考虑到您传递了list.get(position),您只需执行@Html.DisplayNameFor即可返回包含所有数据的对象。
    6. 我希望这有帮助!

答案 1 :(得分:0)

好的,所以这就是你需要做的事情:

  1. 创建新类:DataModel.java,Data.java和Datum.java
  2. 把它放进去:

    DataModel.java:

    public class DataModel {
        private Data data;
    
        public Data getData ()
        {
                return data;
        }
    
        public void setData (Data data)
        {
                this.data = data;
        }
    
    }
    

    Data.java:

    public class Data {
        private Datum[] datum;
    
        public Datum[] getDatum ()
        {
                return datum;
        }
    
        public void setDatum (Datum[] datum)
        {
                this.datum = datum;
        }
    
    
    }
    

    Datum.java:

    public class Datum{
        private String Name;
    
        private String View;
    
        private String Status;
    
        private String Quantity;
    
        private String Date_Sub;
    
        private String Comment;
    
        private String Customer_ID;
    
        private String Order_ID;
    
        private String imgUrl;
    
        public String getName ()
        {
                return Name;
        }
    
        public void setName (String Name)
        {
                this.Name = Name;
        }
    
        public String getView ()
        {
                return View;
        }
    
        public void setView (String View)
        {
                this.View = View;
        }
    
        public String getStatus ()
        {
                return Status;
        }
    
        public void setStatus (String Status)
        {
                this.Status = Status;
        }
    
        public String getQuantity ()
        {
                return Quantity;
        }
    
        public void setQuantity (String Quantity)
        {
                this.Quantity = Quantity;
        }
    
        public String getDate_Sub ()
        {
                return Date_Sub;
        }
    
        public void setDate_Sub (String Date_Sub)
        {
                this.Date_Sub = Date_Sub;
        }
    
        public String getComment ()
        {
                return Comment;
        }
    
        public void setComment (String Comment)
        {
                this.Comment = Comment;
        }
    
        public String getCustomer_ID ()
        {
                return Customer_ID;
        }
    
        public void setCustomer_ID (String Customer_ID)
        {
                this.Customer_ID = Customer_ID;
        }
    
        public String getOrder_ID ()
        {
                return Order_ID;
        }
    
        public void setOrder_ID (String Order_ID)
        {
                this.Order_ID = Order_ID;
        }
    
        public String getImgUrl ()
        {
                return imgUrl ;
        }
    
        public void setImgUrl (String imgUrl)
        {
                this.imgUrl = imgUrl;
        }
    
    }
    

    然后在课堂上Data d = genson.deserialize(jsonString, DataModel.class);

    Inside DataModel是一个数据实例,里面是你的数据数组