gpt4 book ai didi

java - 将对象添加到 JSONObj

转载 作者:太空宇宙 更新时间:2023-11-04 10:57:39 24 4
gpt4 key购买 nike

我想在 onClick 时将对象添加到 JSONObj 中,并在 ListView 内的另一个 Activity 中显示为 CardView

我尝试了一些东西,但什么也没出现,没有错误,我不知道问题出在哪里。

这是我的代码 JSONObj:

 private void writeJSON(String metodaPlata) throws JSONException {
String numeVanzator = SharedPreference.getString(this, SharedPreference.USER_DATA, SharedPreference.NUME_VANZATOR, "");
String jsonDataFromShared = SharedPreference.getString(this, SharedPreference.APP_DATA, SharedPreference.JSON_DATA, "");


JSONObject jsonData;
JSONArray dateJSON;
JSONObject obj;
JSONArray arrayForList;


if (jsonDataFromShared.equals("")) {

jsonData = new JSONObject();
dateJSON = new JSONArray();
obj = new JSONObject();
arrayForList = new JSONArray();

JSONObject objListaSiModalitate = new JSONObject();

// arrayForList.put(stock_list.toString());

objListaSiModalitate.put("lista", new JSONArray(Util.getInstance().getVanzatorProduse()));
objListaSiModalitate.put("metodaPlata", metodaPlata);

obj.put("data", getDate(calendarData.getTimeInMillis()));
obj.put("numeVanzator", numeVanzator);
obj.put("numarClient", 0);
obj.put("detaliiCos", objListaSiModalitate);

dateJSON.put(obj);
jsonData.put("jsonData", dateJSON.toString());

SharedPreference.putString(this, SharedPreference.APP_DATA, SharedPreference.JSON_DATA, jsonData.toString());

} else {

jsonData = new JSONObject(jsonDataFromShared);
dateJSON = jsonData.getJSONArray("jsonData");

obj = new JSONObject();
JSONObject objListaSiModalitate = new JSONObject();
objListaSiModalitate.put("metodaPlata", metodaPlata);
obj.put("produseSelectate", listaProdusePreview.getAdapter().getCount());
int totalPrice = 0;
for (VanzatorProduse v : Util.getInstance().getVanzatorProduse()) {
int vPrice = Integer.parseInt(v.getPret());
totalPrice = totalPrice + vPrice;
}
obj.put("sumaProduse", totalPrice);
obj.put("data", getDate(calendarData.getTimeInMillis()));
obj.put("numeVanzator", numeVanzator);
obj.put("numarClient", numarVanzare);
obj.put("detaliiCos", objListaSiModalitate);

dateJSON.put(obj);

jsonData.put("jsonData", dateJSON);

System.out.println("jsonData" + dateJSON);
SharedPreference.putString(this, SharedPreference.APP_DATA, SharedPreference.JSON_DATA, jsonData.toString());

}
}

我的JSONManager:

 //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)){

String numarClient = c.getString("numarClient");
String sumaProduse = c.getString("sumaProduse");
String produseselectate = c.getString("produseSelectate");

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

卡片对象:

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

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

public Card() {
}

public String getNumarCumparator() {
return numarCumparator;
}

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

public String getListaProduse() {
return listaProduse;
}

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

public String getSumaProduse() {
return sumaProduse;
}

public void setSumaProduse(String 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;
}

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

}

卡片布局:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:descendantFocusability="beforeDescendants">

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

</RelativeLayout>

ListView 的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray_color">

<ListView
android:id="@+id/listViewVanzatorActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btnAdaugaProduse"
android:layout_below="@+id/textViewDataCurenta"/>

</RelativeLayout>

希望你能理解我的问题..

最佳答案

将数据从 Activity 发送到另一个 Activity 的标准方法是在 Intent 包中传递可分包数据。

  • 使卡片实现可打包
  • 将项目传递给 Intent

    public void startSecondActivity(List<Card> cards) {
    Intent i = new Intent(this, SecondActivity.class);
    i.putParcelableArrayListExtra("EXTRA_CARDS", new ArrayList<Card>(cards));
    startActivity(i);
    }
  • 在第二个 Activity 中像这样检索它

    Bundle bundle = getIntent().getExtras();
    List<Card> cards = (ArrayList<Card>)bundle.getParcelableArrayList("EXTRA_CARDS");

关于java - 将对象添加到 JSONObj,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47221322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com