How to get the position of an item from a listview?

113 views Asked by At

I have a wallpaper app that contains a listview that has the images and you pick an image then set it as a wallpaper but I don't know how to tell which item was picked by the user then I have a broadcast receiver so the wallpaper won't change after powering of the device but I also don't know how to tell it which item was picked.

here is my MainActivty:

public class Set1_Activity extends Activity {

ImageView display;  
ListView list;
Integer[] images = {
        R.drawable.iv1_s, R.drawable.iv2_s, R.drawable.iv3_s, R.drawable.iv4_s,
        .......
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main2);

    display = (ImageView) findViewById(R.id.IView);

    list = (ListView) findViewById(R.id.setOneListView);
    HeartlessAdapter adapter = new HeartlessAdapter(this, images);
    list.setAdapter(adapter);

    Button setWp = (Button) findViewById(R.id.setWp);

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

            switch(position){
               case 0:
                   display.setImageResource(R.drawable.iv1);
                break;
            case 1:
                display.setImageResource(R.drawable.iv2);
                break;
            case 2:
                display.setImageResource(R.drawable.iv3);
                break;
            ........

setWp.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast WpSet = Toast.makeText(Set1_Activity.this,"Wallpaper Set", Toast.LENGTH_SHORT);

            DisplayMetrics metrics = new DisplayMetrics(); 
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int height = metrics.heightPixels; 
            int width = metrics.widthPixels;
            Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), //what should I write here );
            Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(Set1_Activity.this); 
            wallpaperManager.setWallpaperOffsetSteps(1, 1);
            wallpaperManager.suggestDesiredDimensions(width, height);
            try {
              wallpaperManager.setBitmap(bitmap);
              } catch (IOException e) {
              e.printStackTrace();
            }
            WpSet.show();
        }

    });

And Here is my broadcast Receiver:

public class BootReceiver extends BroadcastReceiver {
private static final String TAG="BootReceiver";

@Override public void onReceive(Context context,Intent intent){
try{
        DisplayMetrics metrics = new DisplayMetrics(); 
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        int height = metrics.heightPixels; 
        int width = metrics.widthPixels;
        Bitmap tempbitMap = BitmapFactory.decodeResource(context.getResources(), //what should i type here);
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); 
        wallpaperManager.setWallpaperOffsetSteps(1, 1);
        wallpaperManager.suggestDesiredDimensions(width, height);
        try {
          wallpaperManager.setBitmap(tempbitMap);
          } catch (IOException e) {
          e.printStackTrace();
        }
}catch(Exception e){
    Log.e(TAG,e.toString());
}
}
}
1

There are 1 answers

4
Jorgesys On

Why not using preferences, use this method inside your MainActivity Class to save the position:

Create a class to save/retrieve preference values:

import android.content.Context;

public final class Prefs {

    public static void setWPSelected(Context context, int index_wallpaper){
        context.getSharedPreferences("MY_PREFS", 0).edit().putInt("wallpaper_selected", index_wallpaper).commit();
    }   

    public static int getWPSelected(Context context){       
        return context.getSharedPreferences("MY_PREFS", 0).getInt("wallpaper_selected", 0);
    }       
}

Saving Position:

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

              Prefs.setWPSelected(getApplicationContext(), position); //* Saving position

            switch(position){
               case 0:
...
...
...

Retrieving Position:

...
...
...
@Override public void onReceive(Context context,Intent intent){
try{
        DisplayMetrics metrics = new DisplayMetrics(); 
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        int position = Prefs.getWPSelected(context); //*retrieving position.
...
...
...