Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取阵列适配器在其他活动中的位置_Java_Android_Listview_Onclick - Fatal编程技术网

Java 如何获取阵列适配器在其他活动中的位置

Java 如何获取阵列适配器在其他活动中的位置,java,android,listview,onclick,Java,Android,Listview,Onclick,因此,这是父活动,它有一个列表视图,该视图会根据所选的过滤器进行更改 现在我想要的是,当我单击列表视图中的任何项目时,它应该会打开一个新的活动,并且我成功地对它进行了编码 现在应该做的是,无论创建什么新活动,都应该单击项目的标题,但我不想为每个项目创建不同的布局。简言之,我想要动态的东西 public class Catalogue extends AppCompatActivity implements AdapterView.OnItemSelectedListener { @O

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

现在我想要的是,当我单击列表视图中的任何项目时,它应该会打开一个新的活动,并且我成功地对它进行了编码

现在应该做的是,无论创建什么新活动,都应该单击项目的标题,但我不想为每个项目创建不同的布局。简言之,我想要动态的东西

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) {

    }

}

将接口制作成食物适配器类,如下所示

    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();
        }
    });
从您的案例0的角度来看:

修改您的所有这些:

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

并修改您的

    public String positionfinder(){

        return string;
    }


这里的
yourFoodNameGetter()
是模型类中
foodName
的getter。

请查看
ListView
。我认为您需要进一步研究如何使用适配器实现
ListView
。然后你只需要在你的
FoodClass
活动中通过intent传递数据。见下面我的答案。它会解决你的问题。你能告诉我你正在使用的
新食物(“比萨饼”,“可能吸引每一个人”,R.drawable.test)是什么吗。在这里,这个食品类对第一个参数“Pizza”有一个getter和setter。我提到的那个getter就像你的foodNamegetter
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");
    }