将数据放入Gson并将其显​​示在列表视图中

时间:2017-11-03 10:51:39

标签: android json listview gson

这是我第一次与 Gson 面对面。我的问题将是一个很长的问题,希望你能理解,我还有更多的问题让我被困了大约一个星期。

所以,我有ListView,我从另一个Activtity获取了所选项目。在此ListView下,我有Button,卖家将商品保存到FireBase,由客户购买。

现在的问题是,每当卖家将购买的商品保存到FireBase时,我想枚举(如增量)每个客户并提供客户编号。并在页面顶部显示TextView中的客户数量,其中数字1位于图片中。但是第二天的枚举必须从1开始。所以每天必须从1开始。 在这个ListView中,我计算所选项目并计算价格总和。

enter image description here

但第二个问题是我不知道如何将客户编号计数项项目总和发送到另一个{{1}卡片视图。我想把它放在Gson里面。我尝试了一些东西,但我完全迷失了。而且我的代码在这一刻看起来非常混乱。希望你能理解。

以下是我获取所选项目并使用Toast显示计算的项目项目总和的代码:

ListView

客户编号计算项目项目总和的对象类:

int totalPrice = 0;
            for(VanzatorProduse v : Util.getInstance().getVanzatorProduse())
            {
                int vPrice = Integer.parseInt(v.getPret());
                totalPrice = totalPrice + vPrice;
            }

            Toast.makeText(getApplicationContext(), "Selected products : " + listaProdusePreview.getAdapter().getCount() +"\n"+"Total sum : " + totalPrice , Toast.LENGTH_LONG).show();

            Intent sumaProduse = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
            sumaProduse.putExtra("sumaProduse", totalPrice);
            startActivity(sumaProduse);

            String produseSelectate = String.valueOf(listaProdusePreview.getAdapter().getCount());
            Intent intent = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
            intent.putExtra("produseSelectate", produseSelectate);
            startActivity(intent);

            Util.getInstance().getVanzatorProduse().removeAll(Util.getInstance().getVanzatorProduse());

以下是listview适配器的代码:

public class Card {
private Integer numarCumparator;
private String listaProduse;
private Integer sumaProduse;

public Card(Integer numarCumparator, String listaProduse, Integer sumaProduse) {
    this.numarCumparator = numarCumparator;
    this.listaProduse = listaProduse;
    this.sumaProduse = sumaProduse;
}

public Integer getNumarCumparator() {
    return numarCumparator;
}

public void setNumarCumparator(Integer numarCumparator) {
    this.numarCumparator = numarCumparator;
}

public String getListaProduse() {
    return listaProduse;
}

public void setListaProduse(String listaProduse) {
    this.listaProduse = listaProduse;
}

public Integer getSumaProduse() {
    return sumaProduse;
}

public void setSumaProduse(Integer sumaProduse) {
    this.sumaProduse = sumaProduse;
}

卡片视图的布局:

public class CardArrayAdapter  extends ArrayAdapter<Card> {
private static final String TAG = "CardArrayAdapter";
private List<Card> cardList = new ArrayList<Card>();

static class CardViewHolder {
    TextView line1;
    TextView line2;
    TextView line3;
}

public CardArrayAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}

@Override
public void add(Card object) {
    cardList.add(object);
    super.add(object);
}

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

@Override
public Card getItem(int index) {
    return this.cardList.get(index);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    CardViewHolder viewHolder;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.list_item_card, parent, false);
        viewHolder = new CardViewHolder();
        viewHolder.line1 = (TextView) row.findViewById(R.id.txtViewNumarCumparator);
        viewHolder.line2 = (TextView) row.findViewById(R.id.listaProduse);
        viewHolder.line3 = (TextView) row.findViewById(R.id.sumaProduse);
        row.setTag(viewHolder);
    } else {
        viewHolder = (CardViewHolder)row.getTag();
    }
    Card card = getItem(position);

        viewHolder.line1.setText(card.getNumarCumparator());
        viewHolder.line2.setText(card.getListaProduse());
        viewHolder.line3.setText(card.getSumaProduse());

    return row;
}

这是我对Gson的尝试,我将它放在JSONManager类中:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:descendantFocusability="afterDescendants"
    android:orientation="vertical"
    android:paddingBottom="15dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="15dp">

    <TextView
        android:id="@+id/txtViewNumarCumparator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="NumarCumparator" />

    <TextView
        android:id="@+id/listaProduse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtViewNumarCumparator"
        android:layout_marginTop="10dp"
        android:text="NumarProduse" />

    <TextView
        android:id="@+id/sumaProduse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:text="SumaProdsue" />

我试图从Gson获取数据的活动代码。

    public class JSONManager {

private static JSONManager instance = null;



protected JSONManager() {
    // Exists only to defeat instantiation.
}

public static JSONManager getInstance() {
    if (instance == null) {
        instance = new JSONManager();
    }
    return instance;
}


//Create list of Cards because we need to show list for date selected
public List<Card> readJSON(String dateFromCalendar) throws JSONException {

    String JSON = SharedPreference.getString(Util.get(), SharedPreference.APP_DATA, SharedPreference.JSON_DATA, "");
    String numeVanzator = SharedPreference.getString(Util.get(), SharedPreference.USER_DATA, SharedPreference.NUME_VANZATOR,"");

    JSONObject jsonObj = new JSONObject(JSON);

    JSONArray contacts = jsonObj.getJSONArray("jsonData");
    List<Card> listCard = null;
    Card card = null;

    listCard = new ArrayList<>();


    for (int i =0; i< contacts.length();i++) {

        JSONObject c = contacts.getJSONObject(i);
        String data = c.getString("data");

        if (data.equals(dateFromCalendar)){
            String numeVanzato = c.getString("numeVanzator");
            if (numeVanzato.equals(numeVanzator)){

                Integer numarClient = c.getInt("numarClient");

                JSONObject detaliiCos = c.getJSONObject("detaliiCos");
                Integer sumaProduse = detaliiCos.getInt("sumaProduse");
                String produseselectate = c.getString("produseSelectate");

                card = new Card(numarClient, produseselectate, sumaProduse);
                listCard.add(card);
            }
        }
    }
    return listCard;
}  }

目前,一切工作都很完美,但点击保存 @Override protected void onResume() { super.onResume(); c = Calendar.getInstance(); String numeVanzator = SharedPreference.getString(this, SharedPreference.USER_DATA, SharedPreference.NUME_VANZATOR, ""); if (!numeVanzator.equals("")) { textViewNumeVanzator.setText(numeVanzator); } String jsonData = SharedPreference.getString(getApplicationContext(), SharedPreference.APP_DATA, SharedPreference.JSON_DATA, ""); System.out.println("JSON data" + jsonData); textViewDataCurenta.setText(getDate(c.getTimeInMillis())); cardArrayAdapter = new CardArrayAdapter(this, R.layout.list_item_card); Card card = null; List<Card> listCard = new ArrayList<Card>(); c = Calendar.getInstance(); try { String date = getDate(c.getTimeInMillis()); listCard = JSONManager.getInstance().readJSON(date); } catch (JSONException e) { e.printStackTrace(); } for (int i = 0; i <= listCard.size()-1; i++) { card = listCard.get(i); cardArrayAdapter.add(card); } listViewVanzatorActivity.setAdapter(cardArrayAdapter); } 下次活动中没有任何内容,也没有错误。

希望,你了解我的问题,我真的需要你的帮助。谢谢!!

1 个答案:

答案 0 :(得分:0)

在您的代码中,您为调用活动创建了两次Intent,这不是一个好方法。 只需删除此

中的第二个意图
 Intent sumaProduse = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
 sumaProduse.putExtra("sumaProduse", totalPrice);
 startActivity(sumaProduse);


 Intent intent = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
 intent.putExtra("produseSelectate", produseSelectate);
 startActivity(intent);

现在创建新的bundle并使用这样的键添加pass:

 Intent intent = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
 Bundle bundle = new Bundle();
 bundle.put("key",totalPrice);
 bundle.put("key",produseSelectate);
 intent .putExtras(bundle)
 startActivity(intent);

您从JSON获取的列表未存储在共享首选项中。 希望你明白我的观点。