基本上,我在删除删除功能时遇到了一些问题。用户单击ListView中的按钮,然后弹出PopupMenu并显示“Unfriend”,“Unfollow”,“Messgae”和“Cancel”。当用户点击“Unfriend”时,将删除用户想要解除的朋友。
我有三件事有问题;搜索和删除(稍微程度):
1 当用户使用搜索功能时,Unfriend选项将变为不可用。如果用户点击unfriend。它什么都没做...... 2 当用户尝试搜索的用户数超过以前作为朋友删除的用户时。它实际上在搜索结果中显示该用户 3 当用户删除朋友,然后搜索要删除的其他朋友时。它将所有先前删除的朋友添加回列表中。
一定有什么我做错了吗?也许我正在存储数据,然后在某个其他地方重新使用它?
我不会从此代码中获得任何异常。它实际上运行时有轻微错误。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res = getResources();
searchField = (EditText) findViewById(R.id.EditText01);
list = (ListView) findViewById(android.R.id.list);
//button = (Button)findViewById(R.id.btnFriendList);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//button.setFocusable(false);
friendList = 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[i]);
}
descr = new ArrayList<String>();
for(int i = 0; i < descBlock.length; i++) {
descr.add((String)descBlock[i]);
}
images = new ArrayList<Integer>();
for(int i = 0; i < imageBlock.length; i++) {
images.add((Integer)imageBlock[i]);
}
//imageBlock = res.getIntArray(R.array.images);
int size = nameBlock.length;
for(int i = 0 ; i < size; i++) {
Log.d("FREINDADD", "Freind Added" + i);
friendList.add(new Friend(names.get(i), descr.get(i), images.get(i)));
}
Log.i("Application", "Application started succesfully...");
adapter = new VirtuAdapter(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)
{
friendList.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... ");
friendList.add(new Friend(names.get(i), descr.get(i), images.get(i)));
}
}
}
AppendList(friendList);
}
});
}
public void AppendList(ArrayList<Friend> freind) {
setListAdapter(new VirtuAdapter(this));
}
public class VirtuAdapter extends BaseAdapter
{
Activity content;
public VirtuAdapter(Activity context)
{
this.content = context;
}
@Override
public int getCount()
{
return friendList.size();
}
@Override
public Object getItem(int position)
{
return friendList.get(position);
}
@Override
public long getItemId(int position)
{
return friendList.size();
}
class ViewHolder {
TextView myTitle;
TextView myDescription;
ImageView myImage;
Button myButton;
ViewHolder(View view) {
myImage = (ImageView)view.findViewById(R.id.imageview);
myTitle = (TextView)view.findViewById(R.id.title);
myDescription = (TextView)view.findViewById(R.id.mutualTitle);
button = (Button)view.findViewById(R.id.btnFriendList);
}
}
@Override
public View getView(final int position, View view, ViewGroup parent)
{
View row = view;
ViewHolder holder = null;
if(row == null)
{
// If it is visible to the user, deploy the row(s) - allocated in local memory
LayoutInflater inflater = (LayoutInflater)content .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 = friendList.get(position);
friend = friendList.get(position);
String length = "SIZE:" + adapter.getCount();
System.out.println("SIZE"+ length);
Log.d("VIRTU", length);
// Set the resources for each component in the list
holder.myImage.setImageResource(temp.getImage());
holder.myTitle.setText(temp.getName());
holder.myDescription.setText(temp.getDesc());
((Button)row.findViewById(R.id.btnFriendList)).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());
friend.setPosition(position);
Toast.makeText(content, "" + position, Toast.LENGTH_SHORT ).show();
pop.show();
pop.setOnMenuItemClickListener(MainActivity.this);
}
});
return row;
}
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int choice = item.getItemId();
switch(choice) {
case R.id.message:
Toast.makeText(getApplicationContext(),"Clicked on Message", Toast.LENGTH_SHORT).show();
break;
case R.id.unfollow:
Toast.makeText(getApplicationContext(),"Clicked on Unfollow", Toast.LENGTH_SHORT).show();
break;
case R.id.unfriend:
Toast.makeText(getApplicationContext(),"Clicked on Unfreind", Toast.LENGTH_SHORT).show();
Friend temp = friendList.get(friend.getPosition());
friendList.remove(temp);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
break;
case R.id.cancel:
Toast.makeText(getApplicationContext(),"Clicked on Cancel", Toast.LENGTH_SHORT).show();
break;
default: break;
}
return false;
}
}
答案 0 :(得分:0)
问题在于position
的存储方式。当调用按钮单击侦听器时,position
值不是单击该按钮的行。要获得正确的值,您需要在调用getView()时保存ViewHolder对象中的位置,并将按钮单击侦听器移动到if(row == null)
块中。在每次调用getView()时都不需要设置click侦听器。只需在创建布局时设置它。要了解如何完成此操作,请参阅类似问题的answer。