Android OnItemClick Switch Case Toast 消息

Android OnItemClick Switch Case Toast Message

我正在尝试做到这一点,当用户点击图像时,会弹出一个带有相关名称的 Toast。我想我需要使用 switch 语句来执行此操作,因为我有一个数组,但我不确定这是否真的是正确的方法。

这是我的代码:

public class MainActivity extends AppCompatActivity {
    Integer[] Family = {R.drawable.angelica, R.drawable.dad, R.drawable.enzoandbully, R.drawable.enzokathyalex, R.drawable.ernesto, R.drawable.gale, R.drawable.joel, R.drawable.lorenzo};
    ImageView pic;
    Integer member;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView grid = (GridView) findViewById(R.id.gridView);
        final ImageView pic = (ImageView) findViewById(R.id.imgFamily);
        grid.setAdapter(new ImageAdapter(this));
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getBaseContext(), " " + (position + 1), Toast.LENGTH_SHORT).show( );
                pic.setImageResource(Family[position]);
            }
        });
    }
    public class ImageAdapter extends BaseAdapter {
        private Context context;
        public ImageAdapter(Context c) {
            context = c;
        }

        @Override
        public int getCount() {
            return Family.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            pic = new ImageView(context);
            pic.setImageResource(Family[position]);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
            pic.setLayoutParams(new GridView.LayoutParams(330,300));
            return pic;
        }
    }


}

编辑:忘记了 XML 代码:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp" tools:context=".MainActivity">

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:id="@+id/gridView"
        android:numColumns="2"
        android:columnWidth="160dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center" />

    <ImageView
        android:id="@+id/imgFamily"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@string/imgFamily"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_row="12"
        android:layout_column="0" />
</GridLayout>
    int id = item.getItemId();
    switch (item.getItemId()) {
    case R.id.action_add_faculty:
        // EITHER CALL THE METHOD HERE OR DO THE FUNCTION DIRECTLY
        Toast.makeText(Cureent.this,"You clicked yes button",Toast.LENGTH_LONG).show();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

根据您的要求进行更改。希望它会起作用。

这样做:

public class MainActivity extends AppCompatActivity {
    int[] Family = {R.drawable.angelica, R.drawable.dad, R.drawable.enzoandbully, R.drawable.enzokathyalex, R.drawable.ernesto, R.drawable.gale, R.drawable.joel, R.drawable.lorenzo};
    ImageView pic;
    int member;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView grid = (GridView) findViewById(R.id.gridView);
        final ImageView pic = (ImageView) findViewById(R.id.imgFamily);
        grid.setAdapter(new ImageAdapter(this));
        grid.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int iden = view.getId();
        switch(iden) {
        case R.id.gridView:
            Toast.makeText(MainActivity.this, String.valueOf(position + 1), Toast.LENGTH_SHORT).show();
            pic.setImageResource(Family[position]);
            break;

        default:
            break;
        }
    }


    public class ImageAdapter extends BaseAdapter {
        private Context context;
        public ImageAdapter(Context c) {
            context = c;
        }

        @Override
        public int getCount() {
            return Family.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            pic = new ImageView(context);
            pic.setImageResource(Family[position]);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
            pic.setLayoutParams(new GridView.LayoutParams(330,300));
            return pic;
        }
    }
}

希望这对您有所帮助。有问题请评论。

希望这能奏效。

创建一个模型 class 来保存这个家庭成员的姓名和图像 -

public class FamilyMember {
    private String name;
    private int imageResource;

    public FamilyMember(String name, int imageResource) {
        this.name = name;
        this.imageResource = imageResource;
    }

    public String getName() {
        return name;
    }

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

    public int getImageResource() {
        return imageResource;
    }

    public void setImageResource(int imageResource) {
        this.imageResource = imageResource;
    }
}

你的 activity 应该会喜欢这个 -

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

// please use the correct R
import com.sample.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {


    List<FamilyMember> members = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        members = getFamilyMember();
        setContentView(R.layout.activity_main);
        GridView grid = (GridView) findViewById(R.id.gridView);
        final ImageView pic = (ImageView) findViewById(R.id.imgFamily);
        grid.setAdapter(new ImageAdapter(this));
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FamilyMember member = members.get(position);
                Toast.makeText(MainActivity.this, member.getName(), Toast.LENGTH_SHORT).show( );
                pic.setImageResource(member.getImageResource());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class ImageAdapter extends BaseAdapter {
        private Context context;
        public ImageAdapter(Context c) {
            context = c;
        }

        @Override
        public int getCount() {
            return members.size();
        }

        @Override
        public Object getItem(int position) {
            return members.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            FamilyMember member = (FamilyMember) getItem(position);
            pic = new ImageView(context);
            pic.setImageResource(member.getImageResource());
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
            pic.setLayoutParams(new GridView.LayoutParams(330,300));
            return pic;
        }
    }

    private List<FamilyMember> getFamilyMember() {
        List<FamilyMember> members = new ArrayList<>();
        members.add(new FamilyMember("Angelica", R.drawable.R.drawable.angelica));
        members.add(new FamilyMember("Dad", R.drawable.R.drawable.dad));
        // like the above add the family members here
        return members;
    }
}