在另一个活动ListView中显示JSONObject

时间:2017-11-08 13:21:11

标签: java android json listview

我希望 JSON 中的数据显示在 Activity ListView 中>有3列。我为 Layout 制作了 ListView ,但这都是

  <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" />

我有一个 JSONObject 这样的回溯数据:

{"dataCurenta":"11-08-2017",
 "produseSelectate":3,
 "pretTotal":605  
}

以下是代码:

            JSONObject json = new JSONObject();
            try {
                json.put("dataCurenta", getDate(calendarData.getTimeInMillis()));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                json.put("produseSelectate", listaProdusePreview.getAdapter().getCount());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                json.put("pretTotal", totalPrice);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                json.put("numarVanzare", numarVanzare);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.d("LISTA", json.toString());

适配器:

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;
}

public Bitmap decodeToBitmap(byte[] decodedByte) {
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
} }

对象类:

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;
}}

希望问题很明确,我真的需要摆脱这个问题。

0 个答案:

没有答案