• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help with removing in a ListView w/ Search Filter

m trying to create a ListView for a Friends list. It has a search functiton in which tthe user can search for a particular freind and then delete them as a friend, message them and so forth.

However, I'm having trouble removing them. I don't think I understand the positioning, or finding out the correct position on where the users freind is in the list.

I want to make sure that in all cases, the user is removed from the correct position. For instance, if the user uses the search function and only one user is returned. Then I don't want the user to be removed at position 0 (one user), I want it to be removed at the correct position so that when the user goes back to the full list. Position 0 in the list isn't accidentaly removed.

Could someone review the code? and show a slight indication as to where I am going wrong with this?




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

res = getResources();
searchField = (EditText) findViewById(R.id.EditText01);
lv = (ListView) findViewById(android.R.id.list);
//button = (Button)findViewById(R.id.btnFriendList);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//button.setFocusable(false);
list = new ArrayList<Friend>();
friendList2 = new ArrayList<Friend>();

nameBlock = res.getStringArray(R.array.names);
descBlock = res.getStringArray(R.array.descriptions);

names = new ArrayList<String>();
for(int i = 0; i < nameBlock.length; i++) {
names.add((String)nameBlock);
}
descr = new ArrayList<String>();
for(int i = 0; i < descBlock.length; i++) {
descr.add((String)descBlock);
}
images = new ArrayList<Integer>();
for(int i = 0; i < imageBlock.length; i++) {
images.add((Integer)imageBlock);
}
//imageBlock = res.getIntArray(R.array.images);

int size = nameBlock.length;
for(int i = 0 ; i < size; i++) {
Log.d("FREINDADD", "Freind Added" + i);
list.add(new Friend(i, names.get(i), descr.get(i), images.get(i)));
friendList2.add(new Friend(i, names.get(i), descr.get(i), images.get(i)));
//friendList2.add(new Friend(i, names.get(i), descr.get(i), images.get(i)));
}
Log.i("Application", "Application started succesfully...");

adapter = new CustomAdapter(this);


setListAdapter(adapter);
Log.i("VIRTU", "Count" + adapter.getCount());
//adapter.getCount();


searchField.addTextChangedListener(new TextWatcher()
{
@Override public void afterTextChanged(Editable s) {}
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override public void onTextChanged(CharSequence s, int start, int before, int count)
{
list.clear();
textlength = searchField.getText().length();

for (int i = 0; i < names.size(); i++)
{
if (textlength <= names.get(i).length())
{
if(names.get(i).toLowerCase().contains(searchField.getText().toString().toLowerCase().trim())) {
Log.i("VirtuFriendList", "List recyling in process... ");
list.add(new Friend(i, names.get(i), descr.get(i), images.get(i)));

}
}
}
AppendList(list);

}
});


}
public void AppendList(ArrayList<Friend> list) {
setListAdapter(new CustomAdapter(this));
}

class CustomAdapter extends BaseAdapter {
private Context context;
public int pos;
public CustomAdapter(Context context) {
this.context = context;

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

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

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return list.size();
}
class ViewHolder {
TextView userName;
TextView userDesc;
ImageView userImage;
Button userButton;
int position;

ViewHolder(View view) {
userImage = (ImageView)view.findViewById(R.id.imageview);
userName = (TextView)view.findViewById(R.id.title);
userDesc = (TextView)view.findViewById(R.id.mutualTitle);
userButton = (Button)view.findViewById(R.id.btn);
}
}
ViewHolder holder;
View row;
ViewGroup parent;
int position;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
row = convertView;
this.parent = parent;
this.pos = position;
if(row == null)
{
// If it is visible to the user, deploy the row(s) - allocated in local memory
LayoutInflater inflater = (LayoutInflater)context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.search_list_item, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
Log.d("VIRTU", "Row deployed...");
}
else
{
// Recycle the row if it is not visible to to the user - store in local memory
holder = (ViewHolder)row.getTag();
Log.d("VIRTU", "Row recycled...");
}
Friend temp = list.get(position);

// Set the resources for each component in the list
holder.userImage.setImageResource(temp.getImage());
holder.userName.setText(temp.getName());
holder.userDesc.setText(temp.getDesc());
holder.position = temp.getId();

((Button)row.findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
PopupMenu pop = new PopupMenu(getApplicationContext(), v);
MenuInflater inflater = pop.getMenuInflater();

inflater.inflate(R.menu.firned_popup_action,pop.getMenu());
pop.show();
pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {

private int pos = holder.position;

@Override
public boolean onMenuItemClick(MenuItem item) {
int choice = item.getItemId();

switch(choice) {
case R.id.message:
break;

case R.id.unfollow:
break;
case R.id.unfriend:
removeById(0);
adapter = new CustomAdapter(context);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
break;

case R.id.cancel:
}

return false;
}
});
}
});

return row;
}
public void removeById(Object id) {
if(id == null) {
return;
} else {
for(int i = 0; i < list.size(); i++) {
if(id.equals(list.get(i).getId())) {
list.remove(i);
names.remove(i);
images.remove(i);
descr.remove(i);
}
}
}

}

}
}[/HIGH]
 
Back
Top Bottom