【问题标题】:How to set LIstView with multiple arrays/strings?如何使用多个数组/字符串设置 LIstView?
【发布时间】:2014-08-07 15:47:10
【问题描述】:

这是我的代码

我想添加此方法但是,我会避免它们在每次单击按钮时出现相同的 url

所以我希望每个点击的按钮都会改变要显示的网址

我想添加这段代码

     mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
            if (position == 1)
            {
                Intent myIntent = new Intent(getApplicationContext(), SecondaActivity.class);
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("URL")));
            }
                else if (position == 2)
                {Intent myIntent = new Intent(getApplicationContext(), SecondaActivity.class);
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("URL")));

活动A

ArrayList<String> bottone;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //creazione fullscreen activity
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.gruppipuntate_activity);

    //rimozione action bar
    if (Build.VERSION.SDK_INT < 11){
        getSupportActionBar().hide();

        b1 = (Button) findViewById (R.id.button1);
        b2 = (Button) findViewById (R.id.button2);
        b3 = (Button) findViewById (R.id.button3);
        b4 = (Button) findViewById (R.id.button4);
        b5 = (Button) findViewById (R.id.button5);
        b6 = (Button) findViewById (R.id.button6);
        b7 = (Button) findViewById (R.id.button7);
        b8 = (Button) findViewById (R.id.button8);
        b9 = (Button) findViewById (R.id.button9);
        b10 = (Button) findViewById (R.id.button10);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
        b5.setOnClickListener(this);
        b6.setOnClickListener(this);
        b7.setOnClickListener(this);
        b8.setOnClickListener(this);
        b9.setOnClickListener(this);
        b10.setOnClickListener(this);
    }

 }
    //gestione Switch java per selezione puntate
    public void onClick(View v) {
        String[] Product;

        Intent episodi = new Intent(GruppiPuntateActivity.this, EpisodiActivity.class);

         switch(v.getId()){ 
         case R.id.button1:
             Product = new String[]{"ciao", "1", "bottone"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
             break;
         case R.id.button2:
             Product = new String[]{"ciao", "2", "bottone"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
             break;
         case R.id.button3:
             Product = new String[]{"ciao", "3", "bottone"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
             break;
         case R.id.button4:
             Product = new String[]{"asd", "seco", "tezo"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
              break;
         case R.id.button5:
             Product = new String[]{"primo", "secondo", "terzo"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
              break;
         case R.id.button6:
             Product = new String[]{"primo", "secondo", "terzo"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
              break;
         case R.id.button7:
             Product = new String[]{"primo", "secondo", "terzo"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
              break;
         case R.id.button8:
             Product = new String[]{"primo", "secondo", "terzo"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
              break;
         case R.id.button9:
             Product = new String[]{"primo", "secondo", "terzo"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
              break;
         case R.id.button10:
             Product = new String[]{"primo", "secondo", "terzo"};
             episodi.putExtra("Product", Product);
             startActivity(episodi);
              break;
         }
    }

活动 B

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //creazione fullscreen activity
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.episodi_activity);

    final ListView mylist = (ListView) findViewById(R.id.listView1);

    String[] PP = getIntent().getStringArrayExtra("Product"); 

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, PP);
    mylist.setAdapter(adapter);


mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
        if (position == 1)
        {
            Intent myIntent = new Intent(getApplicationContext(), EpisodiActivity.class);
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.dailymotion.com/video/x1zfa4i_camera-cafe-3-stagione-ep-240-crema-miracolosa_shortfilms")));

        }
    }
});

} }

【问题讨论】:

  • 您的问题解决了吗?请不要忘记投票并接受对您最有帮助的答案。
  • 别担心,我试试你的代码,如果它有效,你投票:)

标签: android arrays eclipse listview android-intent


【解决方案1】:

你可以像这样创建一个新的类来保存ListView 中每个项目的数据:

public class ViewModel {
    private String url;
    private String name;

    public ViewModel(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public String getUrl() {
        return this.url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return this.name;
    }
}

toString()返回的值就是ListView中显示的值!要在 ArrayAdapter 中使用此 ViewModel 类,您必须这样做:

// First we create the ViewModels for all the items in the ListView
ViewModel first = new ViewModel("first", "http://www.dailymotion.com/video/x1zfa4i_camera-cafe-3-stagione-ep-240-crema-miracolosa_shortfilms");
ViewModel second = new ViewModel("second", "http://www.dailymotion.com/video/x1zfarh_camera-cafe-3-stagione-ep-241-fuori-di-casa_shortfilms");
ViewModel third = new ViewModel("third", "http://www.dailymotion.com/video/x1zfbes_camera-cafe-3-stagione-ep-242-la-mamma-di-pippo_shortfilms");

// Then we put all the items in a List
List<ViewModel> models = Arrays.asList(first, second, third);

// And finally create an ArrayAdapter which accepts those items
ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(getActivity(), android.R.layout.simple_list_item_1, models);
mylist.setAdapter(adapter);

然后你可以像这样实现OnItemClickListener

mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {

        // We get the ViewModel at the clicked position and get its url
        ViewModel model = (ViewModel) arg0.getItemAtPosition(position);
        String url = model.getUrl();

        // And then we start the next Activity with the correct url
        Intent myIntent = new Intent(getApplicationContext(), EpisodiActivity.class);
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    }
});

您可以像这样轻松地将它应用到您的代码中:

Activity A 中,您首先必须将要打开的 URL 与每个列表项一起放在 String[] 中,并将其传递给 Activity B:

public void onClick(View v) {
    String[] Product;
    String[] urls;

    Intent episodi = new Intent(GruppiPuntateActivity.this, EpisodiActivity.class);

    switch (v.getId()) {
        case R.id.button1:
            Product = new String[]{"ciao", "1", "bottone"};
            urls = new String[] {"url1, url2, url3"};
            episodi.putExtra("Product", Product);
            episodi.putExtra("urls", urls);
            startActivity(episodi);
            break;
        case R.id.button2:
            Product = new String[]{"ciao", "2", "bottone"};
            urls = new String[] {"url4, url5, url6"};
            episodi.putExtra("Product", Product);
            episodi.putExtra("urls", urls);
            startActivity(episodi);
            break;
        ...
    }
}

Activity B 中,您必须阅读产品名称和网址,然后为每个应该显示的项目创建一个ViewModel

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    // We get all the information we need, names and urls
    String[] products = getIntent().getStringArrayExtra("Product");
    String[] urls = getIntent().getStringArrayExtra("urls");

    // And in this loop we create the ViewModel instances from 
    // the name and url and add them all to a List
    List<ViewModel> models = new ArrayList<ViewModel>();
    for (int i = 0; i < products.length; i++) {
        String name = products[i];
        String url = urls[i];
        ViewModel model = new ViewModel(name, url);
        models.add(model);
    }

    // Here we create the ArrayAdapter and assign it to the ListView
    // We pass the List of ViewModel instances into the ArrayAdapter
    final ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(this, android.R.layout.simple_list_item_1, models);
    mylist.setAdapter(adapter);

    mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {

            // Here we get the ViewModel at the given position
            ViewModel model = (ViewModel) arg0.getItemAtPosition(position);

            // And the url from the ViewModel
            String url = model.getUrl();

            // Then we start the other Activity with the correct url
            Intent myIntent = new Intent(getApplicationContext(), EpisodiActivity.class);
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    });
}

差不多就是这样!如果您有任何其他问题,请随时提出!

【讨论】:

  • 所以我把你刚刚写的代码复制粘贴一下?
  • 基本上是的,为什么?当然,您需要填写所有缺失的信息。我原来的答案基本完成了。我现在所做的只是演示如何应用它。
  • 好的,谢谢,我只有最后一个问题。给我一个错误 ViewModel 来解决这个问题?
  • 您评论的最后一部分甚至不是一句话。不明白你的意思。
  • 对不起。我插入 Activity B 的代码向我显示了书面 ViewModel 上的错误我该如何解决?
【解决方案2】:

有点难以理解您要做什么,但我猜您想要做的是根据在活动 A 中单击的按钮更改要在活动 B 中显示的 URL。您可以做的是添加另一个在每个按钮情况下调用 putExtra。这个额外的将是字符串形式的 URL,您将在活动 B 中使用 getStringExtra 再次获取。更好地解释您的问题或目标会有所帮助。

【讨论】:

  • 你猜到了我的意图,你能用几行代码告诉我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多