将自定义ListView项目传递给Android中的其他活动

问题描述 投票:0回答:5

所以我的新闻应用程序有一个自定义列表视图与解析的缩略图,标题和新闻网址,现在我希望将这三个项目传递到另一个活动使用意图,必须显示列表中的特定新闻项目与完整的新闻详情,请帮助我..

android json android-intent android-listview custom-lists
5个回答
0
投票

只需使用Intent将数据从ListView传递到详细信息页面:

Intent i = new Intent(FirstScreen.this, DetailScreen.class);   
String title = "title";
String details = "details";
.....
i.putExtra("STRING_TITLE", title);
i.putExtra("STRING_DETAILS", details);
...

传递您需要的数据。

在DetailScreen.class中接收Intent

String titleString,....;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        titleString= null;
    } else {
        titleString= extras.getString("STRING_TITLE");
        ....
    }
} else {
    titleString= (String) savedInstanceState.getSerializable("STRING_TITLE");
      .....
}

从TextView中检索文本,如下所示:

String title = ((TextView) view.findViewById(your title textview id)).getText().toString();
                Intent newsIntent = new Intent(getApplicationContext(), NewsDetails.class);
                newsIntent.putExtra("title",title);
                startActivity(newsIntent);

0
投票

在Activity之间传递时,传递自定义对象需要实现Parcelable。下面的Intent类方法允许传递parcelable对象。

putExtra(String name, Parcelable value)

以下方法允许传递完整的数据列表

putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

您可以在http://www.survivingwithandroid.com/2015/05/android-parcelable-tutorial-list-class.html上查看博客中的parcelable实现

您可以自己谷歌类似的解决方案。


0
投票

对于那些可能遇到类似问题的人来说,这就是我在同一自定义列表视图中发送多个项目的方式

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent newsIntent = new Intent(getApplicationContext(),NewsDetails.class);
            titleText = (TextView) view.findViewById(R.id.newstitle);
            TextView urlText = (TextView) view.findViewById(R.id.url);
            title = titleText.getText().toString();
            feedUrl = urlText.getText().toString();
            newsIntent.putExtra("title",title);
            newsIntent.putExtra("url",feedUrl);
            startActivity(newsIntent);
        }
    }); 

0
投票

试试这个传递Url:

 @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {


        Uri uri = Uri.parse(myRssFeed.getItem(position).getLink());
        Intent w = new Intent(this, Contenturl.class);
        w.putExtra(org.rss.mywindows.Contenturl.URL,uri.toString());
        startActivity(w);

在您想要的其他活动中试试这个:

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.contenturl);

            String turl = getIntent().getStringExtra(URL);

            Uri feedUri = Uri.parse(turl);

标题:我认为它的字符串因此不容易从意图中传递出来。

如果你想传递对象,那么你应该使用Parcelable Search,你可以更好地将所有事物逐个传递给意图。


-1
投票

我在编程世界中仍然是新手,但在这种情况下我所做的是将这些自定义listview对象存储为我制作的类中的“静态”属性,这纯粹是为了在另一个活动中存储我们需要的东西。

然后启动要转到的活动,并使用存储为自定义ListView对象作为专用数据传输的类的静态属性。

例如:您必须将CustomListview Object传递给另一个Activity:

ActivityOne  {

CustomListView clv = new CustomListView () ;
ObjectTransferClass.customListView = clv;

} 


ObjectTransferClass {

public static MyCustomListView  customListView ;
// other objects to transfer can be declaerd here 


}


ActivityTwo {

MyCustomListView clvFromActivityOne = ObjectTransferClass.customListView;
// use clvFromActivityOne for further processes 
// do other things 

}
© www.soinside.com 2019 - 2024. All rights reserved.