@Override
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;
}
}