我有一个Arraylist。在Arraylist中有两个值Model name和Price。但是有许多重复值。
First Arraylist : Samsung s duos - 10000(Price)
Samsung s duos - 10000
Redmi note 3 - 12000
Redmi note 3 - 12000
我想在另一个Arraylist中的数据(如果找到相同的型号名称时加价)
Second Arraylist: Samsung s duos - 20000(price Adding)
Redmi Note 3 - 24000
这是我的代码......
class Model {
String NAME = "";
String PRICE= "";
}
ArrayList<Model> dataone = new ArrayList<Model>();
这里,这是AsyncTask(背景方法),我把数据放在第一个arraylist ......
while (result.next()) {
Model dataa = new Model();
dataa.NAME = result.getString(1);
dataa.PRICE = result.getString(2);
dataone.add(dataa);
}
这是我的完整代码......
public class ProfitReportItemNameBrandWise extends Activity{
String type, name, toDate, frmDate, brandName;
ArrayList<Model> datas;
List<String> list_header;
ArrayList<Model> dataone;
ListView ll;
Intent intent;
SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profit_report_item_name_brand_wise);
type = getIntent().getStringExtra("type");
Log.e("typ","----"+type);
brandName = getIntent().getStringExtra("name");
Log.e("brand","----"+brandName);
toDate = getIntent().getStringExtra("toDate");
Log.e("todate","----"+toDate);
frmDate = getIntent().getStringExtra("frmDate");
Log.e("frmdate","----"+frmDate);
list_header = new ArrayList<String>();
datas = new ArrayList<Model>();
dataone = new ArrayList<Model>();
pref = getSharedPreferences(new Const().pref, MODE_PRIVATE);
ll = (ListView) findViewById(R.id.listview);
intent = new Intent(ProfitReportItemNameBrandWise.this,ProfitabilityReportFinal.class);
new GetData().execute();
}
public class Model {
String name;
int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
public class GetData extends AsyncTask<Void, String, Boolean> {
ProgressDialog dialog = new ProgressDialog(
ProfitReportItemNameBrandWise.this);
@Override
protected void onPreExecute() {
dialog.setMessage("Please Wait...");
dialog.setCancelable(false);
dialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean result) {
dialog.dismiss();
if (result) {
if (datas.size() > 0) {
Adptor adptor = new Adptor(
ProfitReportItemNameBrandWise.this,
R.layout.textview_list, datas);
ll.setAdapter(adptor);
} else {
Toast.makeText(ProfitReportItemNameBrandWise.this,
"No Data Available", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(ProfitReportItemNameBrandWise.this,
"No Internet Connection", Toast.LENGTH_LONG).show();
}
super.onPostExecute(result);
}
@Override
protected Boolean doInBackground(Void... params) {
try {
SQLConnection conn = new SQLConnection(
ProfitReportItemNameBrandWise.this);
Query qtry = new Query();
String query = qtry.createQuery(
qtry.query,
new String[] { "profitabilityreporttotalwithqty",
pref.getString("username", ""),
pref.getString("company", ""),
pref.getString("ProfitCenter", ""), frmDate,
toDate, brandName, "ALL" });
ResultSet result = conn.getResultFromserver(query);
while (result.next()) {
Model dataa = new Model();
dataa.name = result.getString(3);
dataa.price = result.getInt(7);
dataone.add(dataa);
}
conn.con.close();
if (dataone .size() > 0) {
for (int i = 0; i < dataone.size(); i++) {
for (int j = 0; j < dataone.size(); j++) {
if (dataone.get(i).getName().equals(dataone.get(j).getName())) {
if (datas.size() > 0) {
for (int k = 0; k < datas.size(); k++) {
if (datas.get(k).getName().equals(dataone.get(j).getName())) {
datas.get(k).setPrice(datas.get(k).getPrice() + dataone.get(j).getPrice());
}
else{
datas.add(dataone.get(j));
}
}
}
}
}
}
}
return true;
} catch (Exception e) {
Log.e("error", "" + e);
return false;
}
}
private class Adptor extends ArrayAdapter<Model> {
LayoutInflater inflater;
public Adptor(Context context, int textViewResourceId,
ArrayList<Model> objects) {
super(context, textViewResourceId, objects);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = inflater.inflate(R.layout.textviewsingle, null);
}
LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);
TextView name = (TextView) view.findViewById(R.id.name);
TextView profit = (TextView) view.findViewById(R.id.profit);
ll.setTag(datas.get(position));
name.setText(datas.get(position).name);
profit.setText(datas.get(position).price);
ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Model dats = (Model) v.getTag();
intent.putExtra("name", dats.name);
intent.putExtra("brandName", brandName);
intent.putExtra("toDate", toDate);
intent.putExtra("frmDate", frmDate);
startActivity(intent);
}
});
return view;
}
}
}
}
请帮帮我....谢谢
答案 0 :(得分:0)
将您的Model类设置如下:
public class Model {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
int price;
}
尝试执行以下操作:
if (dataone.size() > 0) {
for (int i = 0; i < dataone.size(); i++) {
boolean flag = false;
for (int j = 0; j < datas.size(); j++) {
if (datas.get(j).getName().equals(dataone.get(i).getName())) {
flag = true;
datas.get(j).setPrice(datas.get(j).getPrice() + dataone.get(i).getPrice());
break;
} else {
flag = false;
}
}
if (!flag) {
datas.add(dataone.get(i));
}
}
}
更改您的
datas.get(k).setPrice(datas.get(k).getPrice() + dataone.get(j).getPrice());
带
datas.get(k).setPrice(String.valueOf(Integer.parseInt(datas.get(k).getPrice()) + Integer.parseInt(dataone.get(j).getPrice())));
答案 1 :(得分:0)
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class DeviceModel implements Parcelable {
private String mModelName;
private String mModelPrice;
public DeviceModel() {
super();
// TODO Auto-generated constructor stub
}
public DeviceModel(String mModelName, String mModelPrice) {
super();
this.mModelName = mModelName;
this.mModelPrice = mModelPrice;
}
public String getmModelName() {
return mModelName;
}
public void setmModelName(String mModelName) {
this.mModelName = mModelName;
}
public String getmModelPrice() {
return mModelPrice;
}
public void setmModelPrice(String mModelPrice) {
this.mModelPrice = mModelPrice;
}
protected DeviceModel(Parcel in) {
mModelName = in.readString();
mModelPrice = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mModelName);
dest.writeString(mModelPrice);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<DeviceModel> CREATOR = new Parcelable.Creator<DeviceModel>() {
@Override
public DeviceModel createFromParcel(Parcel in) {
return new DeviceModel(in);
}
@Override
public DeviceModel[] newArray(int size) {
return new DeviceModel[size];
}
};
}
使用removeDuplicate
删除重复项&amp;添加。
private void removeDuplicates(){
try{
ArrayList<DeviceModel> mListOriginal = new ArrayList<>();
ArrayList<DeviceModel> mListRefined = new ArrayList<>();
for(int i = 0; i < mListOriginal.size(); i++){
boolean isDuplicateFound = false;
int mPrice = Integer.parseInt(mListOriginal.get(i).getmModelPrice());
for(int j = 0 ;j < mListOriginal.size() ;j++){
if(i==j){//Don't check the same index
continue;
}
if(mListOriginal.get(i).getmModelName().toString().equalsIgnoreCase(mListOriginal.get(j).getmModelName())){ //check whether they're same
//Add Price of both - value //priceAdding
isDuplicateFound = true;
mPrice += Integer.parseInt(mListOriginal.get(j).getmModelPrice());//If same add price
}
}
if(!isDuplicateFound){//If no duplicate found, add directly to refined list
mListRefined.add(mListOriginal.get(i));
}else{//If duplicate found
for(int k = 0 ; k< mListRefined.size(); k++){
if(mListRefined.get(k).getmModelName().equalsIgnoreCase(mListOriginal.get(i).getmModelName())){ //check which it was
//Add price only //mListRefined.get(k).addPrice(priceAdding);
mListRefined.get(k).setmModelPrice(String.valueOf(mPrice));//set the price by adding
}
}
}
}
}catch(Exception e){
}
}