如何从ListView中删除项目

时间:2012-01-10 13:24:32

标签: android listview onitemclicklistener onlongclicklistener

我正在构建一个应用程序,其中一个列表视图显示了我最喜欢的粉丝列表。我的粉丝列表是this! 当我点击此列表中的任何项目时,它会向我显示关注范围的完整个人资料,例如this 我的代码是 - >

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);
    }
}

卡住了 - &gt;那,如何从这个列表中删除一个项目(一个粉丝)(favorite_fans_list)...... ???

我尝试在onLongClick Listener上做一些事情,但它不起作用....我很高兴有任何指针或一些样本可以帮助我克服这个问题!!!

2 个答案:

答案 0 :(得分:1)

一种方法是修改适配器中的内容,然后通知列表视图已更改。

答案 1 :(得分:1)

您的适配器为支持它的数据保存一个ArrayList。

您需要从列表中删除该对象。然后通知适配器该组已更改,如下所示。

 favFansList.remove(index);
 adapter.notifyDataSetChanged()