而不是在数组列表中添加新项目,而是覆盖了我应用程序中的现有项目

时间:2018-08-06 00:49:54

标签: android arraylist

我有一个ArrayList,它会不断被覆盖,而不是向列表中添加新值。我不会在每次只创建一次列表时就重新创建它。如果我手动添加详细信息,该数据不会被覆盖,但是当我尝试按该方法添加时,不会填充数据。所以我不确定是什么问题。这是我完整的Main Activity课堂,所有内容都出现了下降。

public class MainActivity extends AppCompatActivity {


ListView listView;
Button button;
public static final String PREFS_NAME = "MyPreferencesFile";
String date_out,date_in;
String part_number,serial_number;
int employee_number;
Context context;
ArrayList<Toolinfo> toolinfos = new ArrayList<>();
String currentDateandTime;
Toolinfo tool1;
public static String TAG = "Main";



ToolListAdapter myadapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myadapter = new ToolListAdapter(this,R.layout.logrowlayout,toolinfos );

    button = findViewById(R.id.button);
    listView =  findViewById(R.id.listview);
    listView.setAdapter(myadapter);
    loadData();

    //Toolinfo too = new Toolinfo("part number","serial number",333,"date","out");
   // Toolinfo too1 = new Toolinfo("part number","serial number",333,"date","out");
   // toolinfos.add(too);
   // toolinfos.add(too1);
    for (int i = 0; i < toolinfos.size() ; i++) {
        Log.d(TAG, "onCreate: tools:" + toolinfos.get(i).getPart_number());


    }

    Intent getData = getIntent();
    if(getData.hasExtra("part number")&& getData.hasExtra("serial number") && getData.hasExtra("employee number")) {
        final String part_number = getData.getStringExtra("part number");
        showToast("has extras");

        String serial_number = getData.getStringExtra("serial number");
         showToast(serial_number);
        int employee_number = getData.getIntExtra("employee number", 0);


        populateData(part_number,serial_number,employee_number,date_out,"out");

     }else {
    showToast("info not present");

}



    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addTool();
        }
    });



}

public void populateData(String part_number,String serial_number,int employee_number, String date_out, String date_in) {
    //loadData();
    SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
    String currentDateandTime = sdf.format(new Date());

    Toolinfo tool = new Toolinfo(part_number, serial_number, employee_number, currentDateandTime, "out");

    myadapter.notifyDataSetChanged();


    for (int i = 0; i < 1 ; i++) {
        //Log.d(TAG, "onCreate: tools:" + toolinfos.get(i).getPart_number());
        toolinfos.add(tool);
    }
    saveData(tool);
}

private  void saveData(Toolinfo tool) {
    SharedPreferences preferences = getSharedPreferences("Shared prefs", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    Gson gson = new Gson();
    String json = gson.toJson(tool);
    editor.putString("toolobj",json);
    showToast(json);
    editor.commit();

    editor.apply();
    showToast("Data Saved");


    }


private void loadData(){
    SharedPreferences preferences = getSharedPreferences("Shared prefs", MODE_PRIVATE);
    Gson gson = new Gson();
    String json = preferences.getString("toolobj","");

    if (json.isEmpty()){
        showToast("no data");

    }else {
        Toolinfo tool1 = gson.fromJson(json, Toolinfo.class);
        if (tool1 != null)
            toolinfos.add(tool1);
        else{

            showToast("Object is null ");
        }

    }

}
private void addTool(){

    Intent i = new Intent(MainActivity.this,Toolsignout.class);
    startActivity(i);
    //saveData(tool1);



}
public void showToast(String msg){
    Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
}

@Override
protected void onResume() {
  //  loadData();
    super.onResume();
}
}

这是arrayadapter类

public class ToolListAdapter extends ArrayAdapter<Toolinfo> {

 private Context mcontext;
int mResource;
ArrayList<Toolinfo> objects;

public ToolListAdapter(Context context, int resource, ArrayList<Toolinfo> objects) {
    super(context, resource, objects);
    mcontext = context;
    mResource = resource;
}

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

    String part_number = getItem(position).getPart_number();
    String serial_number = getItem(position).getSerial_number();
    int employee_number = getItem(position).getEmployee_nummber();
    String date_out = getItem(position).getOut_time();
    String date_in = getItem(position).getIn_time();


    Toolinfo toolinfo = new Toolinfo(part_number,serial_number,employee_number,date_out,date_in);
    LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
    convertView = layoutInflater.inflate(mResource,parent,false);

    TextView parnum = (TextView)convertView.findViewById(R.id.patnum);
    TextView sernum = (TextView)convertView.findViewById(R.id.sernum);
    TextView empnum = (TextView)convertView.findViewById(R.id.empnum);
    TextView timeout = (TextView)convertView.findViewById(R.id.timeout);
    TextView timein = (TextView)convertView.findViewById(R.id.timein);

    parnum.setText(part_number);
    sernum.setText(serial_number);
    empnum.setText(String.valueOf(employee_number));
    timeout.setText((date_out));
    timein.setText((date_in));
    return convertView;

}
}

2 个答案:

答案 0 :(得分:0)

尽管您的代码不易阅读,但我发现在populateData()中,您做了一些莫名其妙的事情:

Toolinfo tool = new Toolinfo(part_number, serial_number, employee_number, currentDateandTime, "out");

myadapter.notifyDataSetChanged();


for (int i = 0; i < 1 ; i++) {
    //Log.d(TAG, "onCreate: tools:" + toolinfos.get(i).getPart_number());
    toolinfos.add(tool);
}

为什么要使用for循环添加1个元素?和myadapter.notifyDataSetChanged();应该在添加后调用:

    Toolinfo tool = new Toolinfo(part_number, serial_number, employee_number, currentDateandTime, "out");
    toolinfos.add(tool);
    myadapter.notifyDataSetChanged();

答案 1 :(得分:0)

在适配器中进行以下更改

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

// **added the if else condition**
         View listItem = convertView;
if (listItem == null)
 {
 LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
convertView = layoutInflater.inflate(mResource,parent,false);


}
String part_number = getItem(position).getPart_number();
String serial_number = getItem(position).getSerial_number();
int employee_number = getItem(position).getEmployee_nummber();
String date_out = getItem(position).getOut_time();
String date_in = getItem(position).getIn_time();


Toolinfo toolinfo = new Toolinfo(part_number,serial_number,employee_number,date_out,date_in);

TextView parnum = (TextView)convertView.findViewById(R.id.patnum);
TextView sernum = (TextView)convertView.findViewById(R.id.sernum);
TextView empnum = (TextView)convertView.findViewById(R.id.empnum);
TextView timeout = (TextView)convertView.findViewById(R.id.timeout);
TextView timein = (TextView)convertView.findViewById(R.id.timein);

parnum.setText(part_number);
sernum.setText(serial_number);
empnum.setText(String.valueOf(employee_number));
timeout.setText((date_out));
timein.setText((date_in));
return convertView;

}

我只是将您的充气机放在if语句中

    View listItem = convertView;
if (listItem == null)
 {
 LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
 convertView = layoutInflater.inflate(mResource,parent,false);
  }