首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用arrayList创建listView?

如何使用arrayList创建listView?
EN

Stack Overflow用户
提问于 2020-12-16 05:46:45
回答 1查看 34关注 0票数 0

当我创建这个列表视图时,我如何将链接分配给我在单击它时创建的每顿饭。我应该为每个元素分配一个标识符,但我尝试了,但它不起作用。

代码语言:javascript
复制
public class elencoPiatti extends AppCompatActivity {
    String link_pasta_lforno = "https://ricette.giallozafferano.it/Pasta-al-forno.html";
    String link_pesto = "https://www.oliocuore.it/prodotti/pesto-alla-genovese-cuore?gclid=Cj0KCQiA2uH-BRCCARIsAEeef3n5A7EWhBRBcaxSrGvMBijLuGX57p5hNu1nb4NG8Vhw_9C2qAfE6kUaAnbbEALw_wcB";
    String link_carbonara = "https://ricette.giallozafferano.it/Spaghetti-alla-Carbonara.html";

ListView listView;

String a = "pasta al forno";
String a1 = "carne";
String a2 = "sugo";
String a3 = "scamorza";
String  a4 ="0" ;

String b = "pesto";
String b1 = "pinoli";
String b2 = "pesto";
String b3 = "pepe";
String b4 = "1";

String r = "carbonara";
String r1 = "uova";
String r2 = "guanciale";
String r3 = "pecorino";
String r4 = "2";

String [][]c = {{"Nome piatto", "Alimento 1", "Alimento2", "Alimento3", "id"},
                {b, b1, b2, b3, b4},
                {a, a1, a2, a3, a4},
                {r, r1, r2, r3, r4}
                };




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.elenco);
//Prendo i paramtri in input
     Bundle primo_alimento=getIntent().getExtras();
     String primo= primo_alimento.getString("first");
     Bundle secondo_alimento=getIntent().getExtras();
     String secondo = secondo_alimento.getString("second");
     Bundle terzo_alimento=getIntent().getExtras() ;
     String terzo= terzo_alimento.getString("terzo");
//
    listView = (ListView) findViewById(R.id.lista);
    ArrayList<String> arrayList = new ArrayList<>();

int contatore =0;for (int i= 1;i <4;i++) {

代码语言:javascript
复制
        for (int j = 1; j < 5 ; j++) {

               if (primo.equals(c[i][j]))
                    {
                 contatore++;
                             }
        if (secondo.equals(c[i][j]))
                            {
                             contatore++;
                            }
                        if (terzo.equals(c[i][j]))
                                {
                                contatore++;
                                 }

        }
                                if (contatore >=2){
                                    String numero = c[i][4];
                                    int intero = Integer.valueOf(numero);
                                    arrayList.add(intero,c[i][0]);
                                    contatore = 0;
                                    }



                                }

    ArrayAdapter arrayAdapter= new ArrayAdapter(this, android.R.layout.simple_list_item_1,arrayList);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (id==0){
                Intent aprilink = new Intent(Intent.ACTION_VIEW, Uri.parse(link_pasta_lforno));
                //Intent intent = new Intent(view.getContext(),pastaalforno.class);

                startActivity(aprilink);

            }
            if (id==1){
                Intent aprilink = new Intent(Intent.ACTION_VIEW, Uri.parse(link_pesto));
                startActivity(aprilink);

            }
            if (id==2){
                Intent aprilink = new Intent(Intent.ACTION_VIEW, Uri.parse(link_carbonara));
                startActivity(aprilink);

            }

        }
    });
    }







}
EN

回答 1

Stack Overflow用户

发布于 2020-12-30 10:57:14

代码语言:javascript
复制
public class YourActivity extends Activity {

private ListView lv;

public void onCreate(Bundle saveInstanceState) {
     setContentView(R.layout.your_layout);

     lv = (ListView) findViewById(R.id.your_list_view_id);

     // Instanciating an array list (you don't need to do this, 
     // you already have yours).
     List<String> your_array_list = new ArrayList<String>();
     your_array_list.add("foo");
     your_array_list.add("bar");

     // This is the array adapter, it takes the context of the activity as a 
     // first parameter, the type of list view as a second parameter and your 
     // array as a third parameter.
     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_list_item_1,
             your_array_list );

     lv.setAdapter(arrayAdapter); 
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65314160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档