Question

I am building one app having One list view showing the list of My favorites Fans. My list of Fans is this! When i click on any item of this list then it show me complete profile of the concern Fan e.g., this My Code is ->

public class FavouriteFansActivity extends ListActivity implements OnItemClickListener, OnLongClickListener {
ListView mFavFansListView;
JSONArray jArrayFavFans;//jArrayFavFans that contains jobjects of all fans. each jobj hv data of 1 unique fan!
JSONObject jFavFan_Data;//contain data of an indivisual fan

LazyAdapter adapter;
ArrayList<Object> favFansList;
ArrayList<String> mfavFansImgs;
ItemBean bean;

String favFans;

//String url="http://XXXXX/ManU/";//Live
String url="http://XXXXX/ManU/";//Local

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.on_favourite_fan_list);

    prepareFavFanArrayLits();//method that prepare list of my favorite fans....

    mFavFansListView = (ListView) findViewById(android.R.id.list);
    adapter = new LazyAdapter(this, mfavFansImgs, favFansList);
    mFavFansListView.setAdapter(adapter);

    mFavFansListView.setOnItemClickListener(this);
    mFavFansListView.setOnLongClickListener(this);

    /** I am still not getting that when to call onPause(), onResume(), onRestart()... etc ??? */

}

/* .........onItemClick......... */
       @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
    // TODO Auto-generated method stub
    ItemBean bean = (ItemBean) adapter.getItem(position);

/**from here-> I am going to start one activity that show the complete profile of a 
     * particular Fan... According to the unique id received from clicking on ListItem!    
     */

    Intent in= new Intent(getParent(), FavFanProfile.class);
    TabGroupActivity prnt = (TabGroupActivity) getParent();

    Bundle fBundle= new Bundle();
    fBundle.putString("fanId", bean.getUid());

    in.putExtras(fBundle);
    prnt.startChildActivity("FavFanProfile", in);

}

/* .........onLongClick......... */
@Override
public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(FavouriteFansActivity.this, "To remove...Clk", Toast.LENGTH_SHORT).show();
    return false;
}


/** Method used to prepare the List of Favorite Fans 
 * @author Rupesh */
public void prepareFavFanArrayLits() {
  /* return me array containing data of all favFans */ 

    Boolean mkFavFansList=false;

    SharedPreferences favFansData = getSharedPreferences("jArrayFavFansPref", MODE_WORLD_WRITEABLE);
    favFans=favFansData.getString("favFansData", "");
    Log.i("FavFans_List->", "FavFans_DATA readed from prefs:"+favFans.toString());
    if(!favFans.equals("")){
        try {
            mkFavFansList=true;
            jArrayFavFans=new JSONArray(favFans);
            favFansList = new ArrayList<Object>();
            mfavFansImgs = new ArrayList<String>();
            Log.i("fav_fansONfav", jArrayFavFans.toString());
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }else {
        Log.i("else_favList_img", "list & image are initialized");
        favFansList = new ArrayList<Object>();
        mfavFansImgs = new ArrayList<String>();
    }

    // ++++++++
    if(mkFavFansList){
        try {
            for (int i = 0; i < jArrayFavFans.length(); i++) {
                // will return the data of each row fetched from JSONArray returned by location1.php
                String data_of_each_user = jArrayFavFans.get(i).toString();

                Log.i("Data Of User at index " + i + " is", data_of_each_user);

                // I put the object at index "i" into JSONObject & retrieve data from name-value pair
                jFavFan_Data = jArrayFavFans.getJSONObject(i);// data of User at index i
                                                    // in array

                AddObjectToList(jFavFan_Data.getString("uniqid").toString(), jFavFan_Data.getString("name"),
                        jFavFan_Data.getString("distance"));

                //Log.i("URL", url+"images/"+jFavFan_Data.get("uniqid").toString()+".png");
                mfavFansImgs.add(url+"images/"+jFavFan_Data.get("uniqid").toString()+".png");
                Log.i("IMG_URL", url+"images/"+jFavFan_Data.get("uniqid").toString()+".png");

                String nm = jFavFan_Data.get("name").toString();
                String uid = jFavFan_Data.get("uniqid").toString();
                String dis = jFavFan_Data.get("distance").toString();

                //System.out.println("Your Name:       " + nm);
                System.out.println("Your Unique Id:  " + uid);
                //System.out.println("Your Distance:   " + dis);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    } else {
        Log.i("NO_FAVORITE_FANS", "No Favorite Fans are added in favorites List!");
        Toast.makeText(FavouriteFansActivity.this, "No Fans in Favorite List!", Toast.LENGTH_SHORT).show();
    }
        // ++++++++++
}

    //**********************setting vales in bean*************************

public void AddObjectToList(String uid, String title, String desc) {
    bean = new ItemBean();
    bean.setUid(uid);
    bean.setDescription(desc);
    bean.setTitle(title);
    favFansList.add(bean);
}

//***********************************************

 @Override
  protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.i("favFansData_FavoriteFansActivity.java", "hi...on resume"+favFans);
    prepareFavFanArrayLits();

    setContentView(R.layout.on_favourite_fan_list);

    mFavFansListView = (ListView) findViewById(android.R.id.list);
    adapter = new LazyAdapter(this, mfavFansImgs, favFansList);
    mFavFansListView.setAdapter(adapter);// come null pointer exception when no fan data is returned! hendle it...

    mFavFansListView.setOnItemClickListener(this);
    }
}

I stuck at the point -> That, how to remove one Item(one Fan) from this list(favorite_fans_list) ...???

I try to do something on onLongClick Listener but it doesn`t work.... I pleased to have any pointer or some sample which help me to overcome from this problem!!!

Was it helpful?

Solution

One way would be to modify the content in the adapter and then notify the listview has changed.

OTHER TIPS

Your adapter holds an ArrayList for the data backing it.

You need to remove the object from the list. Then notify the adapter that the set has changed as follows.

 favFansList.remove(index);
 adapter.notifyDataSetChanged()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top