如何在另一个活动中获取数组适配器的位置

时间:2018-05-14 09:42:20

标签: java android listview onclick

所以这是父活动,它有一个列表视图,根据所选的过滤器进行更改。

现在我想要的是,当我点击列表视图中的任何项目时,它应该打开一个新的活动并且我成功地编写了那个东西。

现在应该发生的是,无论新活动是什么,它都应该点击项目的标题,但我不想为每个项目创建不同的布局。简而言之,我想要一些动态

public class Catalogue extends AppCompatActivity implements AdapterView.OnItemSelectedListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_catalogue);
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.filter_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
        ImageView BackFromCatalogue = (ImageView) findViewById(R.id.BackFromCatalogue);
        BackFromCatalogue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:

                ArrayList<Food> new_food = new ArrayList<Food>();
                new_food.add(new Food("Pizza", "probably attracts evryone", R.drawable.test));
                new_food.add(new Food("noddles", "probably attracts evryone", R.drawable.test));
                new_food.add(new Food("pasta", "probably attracts evryone", R.drawable.test));
                new_food.add(new Food("burger", "probably attracts evryone", R.drawable.test));
                new_food.add(new Food("coke", "probably attracts evryone", R.drawable.test));
                FoodAdapter food_adapter = new FoodAdapter(this, new_food);
                ListView listView = (ListView) findViewById(R.id.food_list);
                listView.setAdapter(food_adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0:
                                Intent i = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i);
                                break;
                            case 1:
                                Intent i1 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i1);
                                break;
                            case 2:
                                Intent i2 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i2);
                                break;
                            case 3:
                                Intent i3 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i3);
                                break;
                            case 4:
                                Intent i4 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i4);
                                break;
                        }
                    }
                });
                break;

            case 1:

                ArrayList<Food> new_food1 = new ArrayList<Food>();

                new_food1.add(new Food("coke", "probably attracts evryone", R.drawable.test));
                new_food1.add(new Food("coke", "probably attracts evryone", R.drawable.test));
                new_food1.add(new Food("coke", "probably attracts evryone", R.drawable.test));
                new_food1.add(new Food("coke", "probably attracts evryone", R.drawable.test));
                new_food1.add(new Food("coke", "probably attracts evryone", R.drawable.test));
                FoodAdapter food_adapter1 = new FoodAdapter(this, new_food1);
                ListView listView1 = (ListView) findViewById(R.id.food_list);
                listView1.setAdapter(food_adapter1);
                listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0:
                                Intent i = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i);
                                break;
                            case 1:
                                Intent i1 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i1);
                                break;
                            case 2:
                                Intent i2 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i2);
                                break;
                            case 3:
                                Intent i3 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i3);
                                break;
                            case 4:
                                Intent i4 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i4);
                                break;
                        }
                    }
                });
                break;

            case 2:

                ArrayList<Food> new_food2 = new ArrayList<Food>();

                new_food2.add(new Food("burger", "probably attracts evryone", R.drawable.test));
                new_food2.add(new Food("burger", "probably attracts evryone", R.drawable.test));
                new_food2.add(new Food("burger", "probably attracts evryone", R.drawable.test));
                new_food2.add(new Food("burger", "probably attracts evryone", R.drawable.test));
                new_food2.add(new Food("burger", "probably attracts evryone", R.drawable.test));
                FoodAdapter food_adapter2 = new FoodAdapter(this, new_food2);
                ListView listView2 = (ListView) findViewById(R.id.food_list);
                listView2.setAdapter(food_adapter2);
                listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0:
                                Intent i = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i);
                                break;
                            case 1:
                                Intent i1 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i1);
                                break;
                            case 2:
                                Intent i2 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i2);
                                break;
                            case 3:
                                Intent i3 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i3);
                                break;
                            case 4:
                                Intent i4 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i4);
                                break;
                        }
                    }
                });
                break;

            case 3:

                ArrayList<Food> new_food3 = new ArrayList<Food>();

                new_food3.add(new Food("pasta", "probably attracts evryone", R.drawable.test));
                new_food3.add(new Food("pasta", "probably attracts evryone", R.drawable.test));
                new_food3.add(new Food("pasta", "probably attracts evryone", R.drawable.test));
                new_food3.add(new Food("pasta", "probably attracts evryone", R.drawable.test));
                new_food3.add(new Food("pasta", "probably attracts evryone", R.drawable.test));
                FoodAdapter food_adapter3 = new FoodAdapter(this, new_food3);
                ListView listView3 = (ListView) findViewById(R.id.food_list);
                listView3.setAdapter(food_adapter3);
                listView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0:
                                Intent i = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i);
                                break;
                            case 1:
                                Intent i1 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i1);
                                break;
                            case 2:
                                Intent i2 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i2);
                                break;
                            case 3:
                                Intent i3 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i3);
                                break;
                            case 4:
                                Intent i4 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i4);
                                break;
                        }
                    }
                });
                break;


            case 4:

                ArrayList<Food> new_food4 = new ArrayList<Food>();

                new_food4.add(new Food("noddles", "probably attracts evryone", R.drawable.test));
                new_food4.add(new Food("noddles", "probably attracts evryone", R.drawable.test));
                new_food4.add(new Food("noddles", "probably attracts evryone", R.drawable.test));
                new_food4.add(new Food("noddles", "probably attracts evryone", R.drawable.test));
                new_food4.add(new Food("noddles", "probably attracts evryone", R.drawable.test));
                FoodAdapter food_adapter4 = new FoodAdapter(this, new_food4);
                ListView listView4 = (ListView) findViewById(R.id.food_list);
                listView4.setAdapter(food_adapter4);
                listView4.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0:
                                Intent i = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i);
                                break;
                            case 1:
                                Intent i1 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i1);
                                break;
                            case 2:
                                Intent i2 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i2);
                                break;
                            case 3:
                                Intent i3 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i3);
                                break;
                            case 4:
                                Intent i4 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i4);
                                break;
                        }
                    }
                });
                break;

            default:

                ArrayList<Food> new_food5 = new ArrayList<Food>();

                new_food5.add(new Food("Pizza", "probably attracts evryone", R.drawable.test));
                new_food5.add(new Food("Pizza", "probably attracts evryone", R.drawable.test));
                new_food5.add(new Food("Pizza", "probably attracts evryone", R.drawable.test));
                new_food5.add(new Food("Pizza", "probably attracts evryone", R.drawable.test));
                new_food5.add(new Food("Pizza", "probably attracts evryone", R.drawable.test));
                FoodAdapter food_adapter5 = new FoodAdapter(this, new_food5);
                ListView listView5 = (ListView) findViewById(R.id.food_list);
                listView5.setAdapter(food_adapter5);
                listView5.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0:
                                Intent i = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i);
                                break;
                            case 1:
                                Intent i1 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i1);
                                break;
                            case 2:
                                Intent i2 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i2);
                                break;
                            case 3:
                                Intent i3 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i3);
                                break;
                            case 4:
                                Intent i4 = new Intent(Catalogue.this, FoodItem.class);
                                startActivity(i4);
                                break;
                        }
                    }
                });
                break;

        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

}

这个底部是单击列表视图中的项目时应打开的活动,其中标题为点击的项目

package com.anonymous.jit.caterers;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class FoodItem extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_item);
        ImageView BackFromCatalogue = (ImageView) findViewById(R.id.BackFromFoodItem);
        BackFromCatalogue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView FITextview = (TextView) findViewById(R.id.app_title);
        FITextview.setText(positionfinder());

    }

    public String positionfinder(){

        return string;
    }
}

2 个答案:

答案 0 :(得分:0)

将接口设置为食物适配器类,如下所示..

    onItemClickListner onItemClickListner;

public void setOnItemClickListner(CommentsAdapter.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner {
    void onClick(int position);//pass your object types.
}

在getView方法中,下面是click事件的代码..

outerContiner.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClickListner.onClick(position);
        }
    });

将适配器绑定到listview后调用下面的代码..

        adapter.setOnItemClickListner(new CommentsAdapter.onItemClickListner() {
        @Override
        public void onClick(int position) {
        Log.d("Postition::",""+position);
        adapter.notifyDataSetChanged();
        }
    });

答案 1 :(得分:0)

从案例0的角度来看:

修改你的所有这些:

Intent i = new Intent(Catalogue.this, FoodItem.class);
startActivity(i);
break;

喜欢

Intent i = new Intent(Catalogue.this, FoodItem.class);
i.putExtra("foodName",new_food.get(position).yourFoodNameGetter());
startActivity(i);
break;

并修改您的

    public String positionfinder(){

        return string;
    }

喜欢

public String positionfinder(){

        return getIntent().getStringExtra("foodName");
    }

此处yourFoodNameGetter()是您在Model类中foodName获取的getter。