如何获取列表视图的所有项目及其位置并将它们意图用于另一个活动,并且可以转到下一个和后退位置
Posted
技术标签:
【中文标题】如何获取列表视图的所有项目及其位置并将它们意图用于另一个活动,并且可以转到下一个和后退位置【英文标题】:how to get all items of listview with them positions and intent them to another activity and can go next and back position 【发布时间】:2020-03-28 07:53:04 【问题描述】:listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
String name = mAdapter.getItem(position).getName();
String title = mAdapter.getItem(position).getTitle();
String url = mAdapter.getItem(position).getUrl();
String link = mAdapter.getItem(position).getLink();
int lastPosition = mAdapter.getCount();
/* for(int i = position;i < lastPosition;i++)
nextName = mAdapter.getItem(i).getName();
nextTitle = mAdapter.getItem(i).getTitle();
nextMusicLink = mAdapter.getItem(i).getLink();
nextImageUrl = mAdapter.getItem(i).getUrl();
*/
int pos = (int) mAdapter.getItemId(position);
int pos1 = mAdapter.getPosition(imageRecord);
String nextName = mAdapter.getItem(position+1).getName();
String nextTitle =mAdapter.getItem(position+1).getTitle();
String nextMusicLink = mAdapter.getItem(position+1).getLink();
String nextImageUrl = mAdapter.getItem(position+1).getUrl();
Bundle extras = new Bundle();
extras.putString("name",nextName);
extras.putString("title",nextTitle);
extras.putParcelableArrayList("list",records);
// extras.putParcelable("list",imageRecord);
//extras.putLong("id",id+1);
extras.putInt("position",position);
extras.putString("url",nextImageUrl);
extras.putString("link",nextMusicLink);
linksModel1.setArtistName(name);
linksModel1.setSongName(title);
linksModel1.setImageUrl(url);
linksModel1.setMusicLink(link);
linksSharedPrefManager = new LinksSharedPrefManager(Objects.requireNonNull(getContext()));
linksSharedPrefManager.saveLInk(linksModel1);
Intent intent = new Intent(getActivity(), music_player.class);
intent.putExtras(extras);
startActivity(intent);
);
这是我的列表视图,我想将带有值的下一个和返回位置发送到另一个活动 使用您看到的下一个位置值发送位置 +1
public class ImageRecord implements Serializable, Parcelable
private String url;
private String title;
private String name;
private String duration;
private String link;
public ImageRecord(String url, String title, String name, String duration, String link)
this.url = url;
this.title = title;
this.name = name;
this.duration = duration;
this.link = link;
protected ImageRecord(Parcel in)
url = in.readString();
title = in.readString();
name = in.readString();
duration = in.readString();
link = in.readString();
public static final Creator<ImageRecord> CREATOR = new Creator<ImageRecord>()
@Override
public ImageRecord createFromParcel(Parcel in)
return new ImageRecord(in);
@Override
public ImageRecord[] newArray(int size)
return new ImageRecord[size];
;
public String getUrl()
return url;
public String getTitle()
return title;
public String getName()
return name;
public String getDuration()
return duration;
public String getLink()
return link;
@Override
public int describeContents()
return 0;
@Override
public void writeToParcel(Parcel parcel, int i)
parcel.writeString(url);
parcel.writeString(title);
parcel.writeString(name);
parcel.writeString(duration);
parcel.writeString(link);
这是我的模型类
public class ImageRecordsAdapter extends ArrayAdapter<ImageRecord>
@Override
public int getPosition(@Nullable ImageRecord item)
return super.getPosition(item);
private final ImageLoader mImageLoader;
ImageRecord imageRecords;
ArrayList<ImageRecord> imageRecordArrayList;
public ImageRecordsAdapter(Context context)
super(context, R.layout.special_list_item);
mImageLoader = new ImageLoader(VolleyApplication.getInstance().getRequestQueue(), new BitmapLruCache());
@Override
public long getItemId(int position)
return super.getItemId(position);
@Override
public int getCount()
return super.getCount();
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent)
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.special_list_item, parent, false);
// NOTE: You would normally use the ViewHolder pattern here
NetworkImageView imageView = (NetworkImageView) convertView.findViewById(R.id.image1);
TextView textView = (TextView) convertView.findViewById(R.id.text1);
TextView textView1 = (TextView) convertView.findViewById(R.id.text2) ;
MediaPlayer m = new MediaPlayer();
ImageRecord imageRecord = getItem(position);
if (imageRecord != null)
imageView.setImageUrl(imageRecord.getUrl(), mImageLoader);
textView.setText(imageRecord.getTitle());
textView1.setText(imageRecord.getName());
try
m.setDataSource(imageRecord.getLink());
catch (IOException e)
e.printStackTrace();
return convertView;
public void swapImageRecords(List<ImageRecord> objects)
clear();
for (ImageRecord object : objects)
add(object);
notifyDataSetChanged();
这是我的适配器
/* nextUrl = (String)intent.getSerializableExtra("url");
nextName = (String)intent.getSerializableExtra("name");
nextTitle = (String)intent.getSerializableExtra("title");
nextLink = (String)intent.getSerializableExtra("link");*/
records = intent.getParcelableArrayListExtra("list");
positionNext =Integer.parseInt(intent.getExtras().get("position").toString());
// imageRecord = (ImageRecord)intent.getParcelableExtra("list");
// id = (int)intent.getLongExtra("id",1);
我曾经在这里得到那个意图
next_btn_music_player.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
nextLink = records.get(positionNext).getLink();
nextName = records.get(positionNext).getName();
nextTitle = records.get(positionNext).getTitle();
nextUrl = records.get(positionNext).getUrl();
linksModel1.setArtistName(nextName);
linksModel1.setSongName(nextTitle);
linksModel1.setImageUrl(nextUrl);
linksModel1.setMusicLink(nextLink);
linksSharedPrefManager = new LinksSharedPrefManager(Objects.requireNonNull(context));
linksSharedPrefManager.saveLInk(linksModel1);
try
backgroundAudioservice.next();
catch (IOException e)
e.printStackTrace();
/* try
backgroundAudioService.next();
catch (IOException e)
e.printStackTrace();
*/
Intent intent1 = new Intent(music_player.this , music_player.class);
startActivity(intent1);
);
并使用下一个按钮将下一个位置值发送到下一个活动
【问题讨论】:
你的问题到底是什么? 假设列表中的项目数量不只少数,您肯定不想将整个项目列表放在Intent
中作为“额外的”。 Intent
的大小是有限的,像这样传递大量数据是行不通的。您最好将项目列表保存在 static
变量中,您的所有代码都可以直接访问该变量。或者将项目列表放入数据库中。
***.com/users/769265/david-wasser 我怎样才能使我的列表视图项目静态?
我想选中下一个项目
在您的Activity
中,当您创建Adapter
时,您会将项目列表传递给它。将其放入 public static
变量中。然后所有类都可以使用Classname.variablename
访问它,您无需传递它。
【参考方案1】:
尝试 +1 位置数据。
int po= position+1;
String nextName = mAdapter.getItem(po).getName();
【讨论】:
以上是关于如何获取列表视图的所有项目及其位置并将它们意图用于另一个活动,并且可以转到下一个和后退位置的主要内容,如果未能解决你的问题,请参考以下文章
使用sqlite意图后如何从listview中获取项目名称?