【问题标题】:Android - How do I launch a separate screen with information based on the selected ListView choice?Android - 如何根据所选的 ListView 选项启动包含信息的单独屏幕?
【发布时间】:2014-03-01 14:26:33
【问题描述】:

我有一个列表视图,它显示设备上所有已安装应用程序的列表。我无法找到一种方法来启动一个单独的屏幕,其中包含基于列表视图中所选项目的信息。

例如:单击列表视图中的“Facebook”将打开一个屏幕,其中包含我输入的有关 Facebook 的信息。

代码

public class ListApplications extends Activity {

private ListView lView;
 private ArrayList results = new ArrayList();

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

  lView = (ListView) findViewById(R.id.listview);
  PackageManager pm = this.getPackageManager();

  Intent intent = new Intent(Intent.ACTION_MAIN, null);
  intent.addCategory(Intent.CATEGORY_LAUNCHER);

  List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);

  for (ResolveInfo rInfo : list) {
   results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
   Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
  } 

 lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results)); 
 }

【问题讨论】:

    标签: android listview android-listview android-activity


    【解决方案1】:

    为您的列表视图设置一个 onItemClicked 侦听器,在 onItemClicked 回调中,您将通过 pos 参数知道单击了哪个项目。并且从您在代码中定义的结果数组中,您将知道下一步该做什么。

    可以参考http://developer.android.com/guide/topics/ui/layout/listview.html官方教程

    【讨论】:

      【解决方案2】:

      我是按位置使用的,你也可以使用所选列表项的文本

       listView.setOnItemClickListener(new OnItemClickListener()
       {
       @Override
       public void onItemClick(AdapterView<?> parent, View view,
       int position, long id) {
      
      Intent intent = new Intent(MainActivity.this,Detailedcontent.class);
      intent.putExtra("selectedItemPosition", position);
      startActivity(intent );
      
      } 
      });
      }
      

      关于Detailedcontent.class

      Bundle bundle = getIntent().getExtras();
      int position=bundle.getInt("selectedItemPosition");
      
      switch(position)
      {
      
      case 1://if incase facebook position is one
      //set content about fb
      
      break;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 2013-12-07
        • 2018-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多