Derek
Derek

Reputation: 632

How do I add activities to a favourites list?

I would like to have a favourites list in my app but I'm not sure how to do it. Basically when a star button is pressed in the menu bar of an activity I would like a custom link/button to bet added to a favourites menu in another activity.

Any help at all is great.

thanks in advance!

Edit here is where I'm at:

public class MainActivity extends Activity implements OnItemClickListener {

ListView lv;
List<ListViewItem> items;
CustomListViewAdapter adapter;

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    ListViewItem item = items.get(position);
    items.remove(item);
    adapter = new CustomListViewAdapter(this, items);
    lv.setAdapter(adapter);
}

public static final String PREFS = "examplePrefs";
String LINK = "MainActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView) findViewById(R.id.ListView);
    items = new ArrayList<MainActivity.ListViewItem>();
    items.add(new ListViewItem()
    {{
        ThumbnailResource = R.drawable.ic_launcher;
        Title = "Item1";
        SubTitle = "Item1 desciption";

    }});
    items.add(new ListViewItem()
    {{
        ThumbnailResource = R.drawable.ic_launcher;
        Title = "Item2";
        SubTitle = "Item2 desciption";

    }});

    adapter = new CustomListViewAdapter(this,items);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(this);

class ListViewItem
{
    public int ThumbnailResource;
    public String Title;
    public String SubTitle;
}

Here is my listview adapter .java

public class CustomListViewAdapter extends BaseAdapter


LayoutInflater inflater;
List<ListViewItem> items;

public CustomListViewAdapter(Activity context, List<ListViewItem> items) {  
    super();

    this.items = items;
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override  
public int getCount() {  
    // TODO Auto-generated method stub  
    return items.size();  
}  

@Override  
public Object getItem(int position) {  
    // TODO Auto-generated method stub  
    return null;  
}  

@Override  
public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return 0;  
}

@Override  
public View getView(final int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  
    ListViewItem item = items.get(position);

    View vi=convertView;

    if(convertView==null)
        vi = inflater.inflate(R.layout.item_row, null);

    ImageView imgThumbnail = (ImageView) vi.findViewById(R.id.imgThumbnail);
    TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
    TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);

    imgThumbnail.setImageResource(item.ThumbnailResource);
    txtTitle.setText(item.Title);
    txtSubTitle.setText(item.SubTitle);



    return vi;  
}

Here is my item row .xml

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout   
android:id="@+id/relativeLayout1"   
android:layout_width="fill_parent"   
android:layout_height="fill_parent"   
xmlns:android="http://schemas.android.com/apk/res/android"  
android:padding="5dip">  

<ImageView   
    android:layout_width="78dip"   
    android:layout_height="78dip"   
    android:id="@+id/imgThumbnail"   
    android:layout_alignParentLeft="true"  
    android:layout_centerInParent="true"
    android:layout_marginLeft="-3dip"
    android:scaleType="centerInside">  
</ImageView>  

<TextView   
    android:textAppearance="?android:attr/textAppearanceLarge"   
    android:layout_height="wrap_content"   
    android:text="TextView"   
    android:layout_width="wrap_content"   
    android:id="@+id/txtTitle"   
    android:layout_toRightOf="@+id/imgThumbnail"
    android:layout_marginTop="6dip"  
    android:layout_marginLeft="6dip">  
</TextView>  

<TextView   
    android:layout_height="wrap_content"   
    android:text="TextView"   
    android:layout_width="wrap_content"   
    android:id="@+id/txtSubTitle"   
    android:layout_toRightOf="@+id/imgThumbnail"  
    android:layout_below="@+id/txtTitle"
    android:layout_marginTop="3dip"  
    android:layout_marginLeft="6dip">  
</TextView>

I'm stuck on trying to get all necessary information to populate a listview item from the Action Bar item that I've added in my activity that is to be faourited.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.favourite, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
    //respond to menu item selection
    switch (item.getItemId()) {
    case R.id.favourite1:

    //this is where shared prefrences is created?

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

Upvotes: 2

Views: 2214

Answers (1)

amalBit
amalBit

Reputation: 12181

My suggestion..

  1. On clicking the fav icon:

    • get the name of the current activity and persist it.(db,sharedpref .. your choice).
  2. Create a listView and an adapter for it, which fetches the activity name from the db or sharedPref.

  3. Set click listener for the list view.

    • get the name of the clicked listview.
    • call an intent with the selected val.

My suggestion..

  1. On clicking the fav icon:

    • get the name of the current activity and persist it.(db,sharedpref .. your choice).
  2. Create a listView and an adapter for it, which fetches the activity name from the db or sharedPref.

  3. Set click listener for the list view.

    • get the name of the clicked listview.
    • call an intent with the selected val.

EDIT:

Sharedpreference is one of the ways of persisting data in android.(others being database,file etc). The sharedPreference file stores in a key-value format.

  1. Get an instance of the sharedpreference class and editor classs:

    SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    SharedPreferences.Editor editor = wmbPreference.edit();

  2. Add values in the sharedPrefrence :

        editor.putBoolean("key", value);
        editor.putFloat("key1", value);
        editor.putInt("key2", value);
        editor.putLong("key3", value);
        editor.putString("key4", value);
        editor.putStringSet("key5", values);
    
  3. Persist these inserted values:

    editor.commit();

Now, these key-value pairs can be utilized from any activity:

  1. Get an instance:

    SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

  2. Get the value by passing the right key:

    boolean boolValue= wmbPreference.getBoolean("key", true);

Thats it, as of sharedprefrence is concerned.

Upvotes: 1

Related Questions