【问题标题】:Passing Custom ListView Items to Other Activities in Android将自定义 ListView 项传递给 Android 中的其他活动
【发布时间】:2016-04-10 03:05:33
【问题描述】:

所以我的新闻应用程序有一个自定义列表视图,其中包含已解析的缩略图、标题和新闻 url,现在我希望使用意图将这三个项目传递给另一个活动,该意图必须显示列表中的特定新闻项目以及完整的新闻详细信息,请帮帮我..

【问题讨论】:

标签: android json android-intent android-listview custom-lists


【解决方案1】:

只需使用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);

【讨论】:

  • 兄弟,我如何将第一个活动的列表视图中的新闻标题获取到第二个活动,我已经尝试过你的方法,但它只显示最后一项的标题列表视图...我需要根据用户点击的列表来获取标题
【解决方案2】:

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

putExtra(String name, Parcelable value)

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

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

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

您可以自己搜索类似的解决方案。

【讨论】:

    【解决方案3】:

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

    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);
            }
        }); 
    

    【讨论】:

      【解决方案4】:

      试试这个传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,这样你会得到更好的方法,然后将所有的东西一一传递。

      【讨论】:

      • 让我试一试..!
      • @a_lmukthar 发生了什么请告诉我。如果这个答案对你有帮助,那么别忘了放弃投票
      • @a_lmukthar 你在 json 中获取数据??
      • 是的,json 解析在列表活动中工作,我想要的是将列表视图中所选项目的标题发送到其他活动......现在我只得到最后一个项目的标题在列表视图中,无论在列表活动中单击的行...
      • @a_lmukthar 你首先解析 Json 并在 Model 类中添加值,然后在 listview 中设置它以便你从模型类中获取项目值
      【解决方案5】:

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

      然后启动您想去的活动,并使用存储为类的静态属性的自定义 ListView 对象,用于专用数据传输。

      例如:您必须将 CustomListview 对象传递给另一个 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 
      
      }
      

      【讨论】:

      • 您能说得更具体些吗?
      • 如果您想从 ActivityTwo 访问 ActivityOne 中的自定义列表视图,则无需执行上述操作。相反,您可以在全局类中将自定义列表视图声明为静态,并且可以在任何您想要的位置访问它。
      • 这是不正确的,建议在活动之间传递数据。
      • 我已经尝试过之前回答过的方法,但是我得到的是列表视图中最后一个列表项的标题,而不管项目单击...即使我单击第一个项目,我得到最后一个项目的标题..这发生在所有情况下..如何使用 OnItemClickListener 中的位置变量获取标题?我该怎么做?
      • 您可以在列表视图的 OnClickListener 上处理索引,其中必须有一个“索引”变量被传递到 onClick(View v, int index) {
      猜你喜欢
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      相关资源
      最近更新 更多