我正在开发一个项目,该项目允许用户使用按钮将字符串从一个活动发送到另一个活动的列表视图。但是,当我尝试将数据从一个活动发送到下一个活动时,接收活动将替换最后发送的项目而不是添加到列表视图。这是我的代码: EnterInfoActivity.java:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import static com.example.kevin.myapplication.R.id.Done;
import static com.example.kevin.myapplication.R.id.EnterInfo;
public class EnterInfoActivity extends AppCompatActivity {
ArrayAdapter<CharSequence> yearAdapter;
Button Done;
EditText Name;
EditText PName;
Spinner YearSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_info);
Name = (EditText) findViewById(R.id.Name);
PName = (EditText) findViewById(R.id.PName);
Done = (Button) findViewById(R.id.Done);
YearSpinner = (Spinner) findViewById(R.id.Year);
yearAdapter = ArrayAdapter.createFromResource(this,R.array.year_sequence, android.R.layout.simple_spinner_item);
yearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
YearSpinner.setAdapter(yearAdapter);
Done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("ViewData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("newName", Name.getText().toString());
editor.putString("newPName", PName.getText().toString());
editor.putString("Year", YearSpinner.getSelectedItem().toString());
editor.commit();
finish();
}
});
}
}
和heres ViewActivity.java:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ListViewCompat;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class ViewActivity extends AppCompatActivity {
ListView lv;
ArrayList<String> arrayList;
ArrayAdapter<String> adapter;
public static final String DEFAULT = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
SharedPreferences sharedPreferences = getSharedPreferences("ViewData", Context.MODE_PRIVATE);
String Name = sharedPreferences.getString("newName",DEFAULT);
String PName = sharedPreferences.getString("newPName",DEFAULT);
String Year = sharedPreferences.getString("Year",DEFAULT);
String PhotoInfo =(Name + " " + PName + " " + Year);
lv = (ListView) findViewById(R.id.LV_View);
arrayList = new ArrayList<String>();
arrayList.add(PhotoInfo);
adapter = new ArrayAdapter<String>(ViewActivity.this, android.R.layout.simple_list_item_1,
arrayList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
在你的arrayList中,你总是拥有最后一个。您没有添加现有列表,因此它只显示最后一个
Sub test()
Dim wdDoc As Word.Document
Dim r As Word.Range
Dim s As String
' Const c As Integer = 19 ' Works when field codes are visible
Const c As Integer = 1 ' Works when objects are visible
Set wdDoc = ActiveDocument
Set r = wdDoc.Content
With r.Find
.Text = "RPDIS" & ChrW(8594) & "*" & ChrW(8592) & "RPDIS"
.MatchWildcards = True
While .Execute
s = r.Text
If InStr(1, s, chr(c), vbTextCompare) > 0 Then
Debug.Print "Delete: " & s
' r.Delete ' This line commented out for testing; remove comments to actively delete
Else
Debug.Print "Keep: " & s
End If
Wend
End With
End Sub
从你可以看到的最后两行开始,你正在创建一个新的列表并只向它添加一个String。因为你可以看到最后一个。
答案 1 :(得分:0)
您必须将 ArrayList设置为静态,并且不要每次都续订!您只需创建一次,每次只需添加新项,然后调用 notifyDatasetChanged()。
public class ViewActivity extends AppCompatActivity {
ListView lv;
//set arraylist as static and eager loading for it.
public static ArrayList<String> arrayList=new ArrayList<String>();
ArrayAdapter<String> adapter;
public static final String DEFAULT = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
SharedPreferences sharedPreferences = getSharedPreferences("ViewData", Context.MODE_PRIVATE);
String Name = sharedPreferences.getString("newName",DEFAULT);
String PName = sharedPreferences.getString("newPName",DEFAULT);
String Year = sharedPreferences.getString("Year",DEFAULT);
String PhotoInfo =(Name + " " + PName + " " + Year);
lv = (ListView) findViewById(R.id.LV_View);
//remove renew arraylist
arrayList.add(PhotoInfo);
adapter = new ArrayAdapter<String>(ViewActivity.this, android.R.layout.simple_list_item_1,
arrayList);
lv.setAdapter(adapter);
//its ok
adapter.notifyDataSetChanged();
}