我有arraylists。我想要那个arraylist的数据。我没有收到数据。相反,当我使用arraylist.get(i)时,我得到了整个arraylist。
这是我的代码
这是listview项目的主要活动,我获取值并使用putStringArrayListExtra将其传递给下一个活动。
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String varr = history.get(position).getLati().toString();
ArrayList<String>clicklat=new ArrayList<String>();
clicklat.add(varr);
String var2 = history.get(position).getLongi().toString();
ArrayList<String>clicklong=new ArrayList<String>();
clicklong.add(var2);
String var3 = history.get(position).getDatetime().toString();
ArrayList<String>dttime=new ArrayList<String>();
dttime.add(var3);
Intent i = new Intent(HistoryActivity.this, DetailsActivity.class);
i.putStringArrayListExtra("clicklat", clicklat);
i.putStringArrayListExtra("clicklong", clicklong);
i.putStringArrayListExtra("clickdatetime", dttime);
startActivity(i);
}
});
这是DetailsActivity
public class DetailsActivity extends AppCompatActivity {
private Toolbar toolbar;
String latitudee;
String longitudee;
String dateetime;
ArrayList<String> newLat;
ArrayList<String> newLong;
ArrayList<String> newDateTime;
ArrayList<DetailsPojo> details;
DetailsPojo detailsPojo;
DetailsAdapter adapter;
ListView detList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setTitle("History Details");
Intent intent = getIntent();
details = new ArrayList<DetailsPojo>();
newLat = new ArrayList<>();
newLong = new ArrayList<>();
newDateTime = new ArrayList<>();
newLat = intent.getStringArrayListExtra("clicklat");
newLong = intent.getStringArrayListExtra("clicklong");
newDateTime = intent.getStringArrayListExtra("clickdatetime");
for (int i = 0; i < newLat.size(); i++) {
detailsPojo = new DetailsPojo();
latitudee = newLat.get(i);
longitudee = newLong.get(i);
dateetime = newDateTime.get(i);
Log.e("detais latitude", "" + latitudee);
Log.e("detailos longitudee", "" + longitudee);
Log.e("detailos datetimeee", "" + dateetime);
detailsPojo.setDetailsdatetime(dateetime);
detailsPojo.setDetailslat(latitudee);
detailsPojo.setDetailslong(longitudee);
}
details.add(detailsPojo);
detList = (ListView) findViewById(R.id.detailsList);
adapter = new DetailsAdapter(DetailsActivity.this, details);
detList.setAdapter(adapter);
}
这是DetailsAdapter
public class DetailsAdapter extends BaseAdapter {
private Context activity;
TextView dt_datetime;
TextView dt_lat;
TextView dt_long;
ArrayList<DetailsPojo> list;
private ArrayList<DetailsPojo> arraylist = null;
public static LayoutInflater inflater;
private Context context;
public DetailsAdapter(Context a, ArrayList<DetailsPojo> details) {
// TODO Auto-generated constructor stub
activity = a;
list = details;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.arraylist = new ArrayList<DetailsPojo>();
this.arraylist.addAll(list);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if(convertView == null) {
v = inflater.inflate(R.layout.details_item, parent, false);
}
final DetailsPojo pojo = list.get(position);
dt_datetime = (TextView) v.findViewById(R.id.detailsdatetime);
dt_lat = (TextView) v.findViewById(R.id.detailslat);
dt_long = (TextView) v.findViewById(R.id.detailslong);
dt_datetime.setText(pojo.getDetailsdatetime());
dt_lat.setText(pojo.getDetailslat());
dt_long.setText(pojo.getDetailslong());
return v;
}
}
这是arraylist
latitude﹕ [21.121776, 21.121776, 21.121776, 21.121776, 21.121776, 21.121776, 21.121776, 21.121776, 21.121776, 21.121776, 21.121776, 21.121776, 21.121776, 21.121774, 21.121774]
更新:
这是getLati和getLongi
public List<String> getLati(){
return this.lat;
}
public List<String> getLongi(){
return this.longi;
}
public void setLati(List<String> lat){
this.lat = lat;
}
public void setLongi(List<String> longi){
this.longi = longi;
}
有什么不对吗?请帮帮我。
答案 0 :(得分:1)
在HereActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
...
for (int i = 0; i < newLat.size(); i++) {
DetailsPojo detailsPojo = new DetailsPojo();
latitudee = newLat.get(i);
longitudee = newLong.get(i);
dateetime = newDateTime.get(i);
Log.e("detais latitude", "" + latitudee);
Log.e("detailos longitudee", "" + longitudee);
Log.e("detailos datetimeee", "" + dateetime);
detailsPojo.setDetailsdatetime(dateetime);
detailsPojo.setDetailslat(latitudee);
detailsPojo.setDetailslong(longitudee);
details.add(detailsPojo); // move it to here
}
// details.add(detailsPojo); // remove it
detList = (ListView) findViewById(R.id.detailsList);
adapter = new DetailsAdapter(DetailsActivity.this, details);
detList.setAdapter(adapter);
}
....
希望这个帮助
答案 1 :(得分:1)
试试这段代码:
protected void onCreate(Bundle savedInstanceState) {
...
for (int i = 0; i < newLat.size(); i++) {
DetailsPojo detailsPojo = new DetailsPojo(); //changes here
latitudee = newLat.get(i);
longitudee = newLong.get(i);
dateetime = newDateTime.get(i);
Log.e("detais latitude", "" + latitudee);
Log.e("detailos longitudee", "" + longitudee);
Log.e("detailos datetimeee", "" + dateetime);
detailsPojo.setDetailsdatetime(dateetime);
detailsPojo.setDetailslat(latitudee);
detailsPojo.setDetailslong(longitudee);
details.add(detailsPojo); // and here
}
detList = (ListView) findViewById(R.id.detailsList);
adapter = new DetailsAdapter(DetailsActivity.this, details);
detList.setAdapter(adapter);
}
我在循环中声明了detailsPojo
对象而不是全局变量。
PS :确保newLong
和newDateTime
的大小与newLat
相同,以避免IndexOutOfBoundsException。
<强>更新强> 将您的点击监听器更改为:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ArrayList<String>clicklat= new ArrayList<String>(history.get(position).getLati());
ArrayList<String>clicklong= new ArrayList<String>(history.get(position).getLongi());
ArrayList<String>dttime= new ArrayList<String>(history.get(position).getDatetime());
Intent i = new Intent(HistoryActivity.this, DetailsActivity.class);
i.putStringArrayListExtra("clicklat", clicklat);
i.putStringArrayListExtra("clicklong", clicklong);
i.putStringArrayListExtra("clickdatetime", dttime);
startActivity(i);
}
答案 2 :(得分:0)
我认为这里的问题是你在onClick监听器中创建列表。这意味着每次用户单击时,都会创建一个新的List,然后添加值。所以你总会得到1个大小的列表。如果你想在1个大小的列表上使用get(),你将得到唯一的元素a.k.a.整个列表。只需按照以下说明移动:
ArrayList<String>clicklat=new ArrayList<String>();
在听众之外,你应该没事。