Friday 6 March 2015

Search list demo

1) AddFriendsFragment.java
======================
package com.boss.fragment;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.boss.FragmentMainActivity;
import com.boss.R;
import com.boss.TextViewPlus;
import com.boss.adapter.AddFriendsListAdapter;
import com.boss.adapter.SearchListAdapter;
import com.boss.logger.Logger;
import com.boss.model.FriendsDataModel;
import com.boss.model.ManageFriendsModel;
import com.boss.model.UserDataModel;
import com.boss.services.AsyncCallListener;
import com.boss.services.FriendsAddedMeRequestTask;
import com.boss.services.GetFriendsListRequestTask;
import com.boss.services.UserSearchRequestTask;
import com.boss.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class AddFriendsFragment extends Fragment implements OnRefreshListener, OnClickListener, TextWatcher{
private View mView;
private ListView mFriendsList, mSearchFriendsList;
private TextView mSiderowTextView;
private Map<String, Integer> sideIndex;
private String[] alphabaticalList = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
private ManageFriendsModel all_f_model = new ManageFriendsModel();
private SearchListAdapter mAdapter;
private AddFriendsListAdapter mFriendsListAdapter;
private ArrayList<UserDataModel> mSearchData = new ArrayList<UserDataModel>();
private ArrayList<FriendsDataModel> mAddFriendsList  = new ArrayList<FriendsDataModel>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_addfriends, null);
initView();
return mView;
}

private void initView() {
mView.findViewById(R.id.Title_Addfriend).setVisibility(View.GONE);
if(FragmentMainActivity.myPager.getCurrentItem() == 2 && FragmentMainActivity.sendtoFlag == 1){
((TextViewPlus)mView.findViewById(R.id.Title)).setText(getResources().getString(R.string.send_to));
}
else if(FragmentMainActivity.myPager.getCurrentItem() == 2 && FragmentMainActivity.sendtoFlag == 0){
((TextViewPlus)mView.findViewById(R.id.Title)).setText(getResources().getString(R.string.my_friend));
}
else {
((TextViewPlus)mView.findViewById(R.id.Title)).setText(getResources().getString(R.string.add_friend));
}

mFriendsList = (ListView)mView.findViewById(R.id.addfriends_listview);
mSearchFriendsList = (ListView)mView.findViewById(R.id.addfriends_search_listview);

mFriendsList.setOnItemClickListener(friendClick);
mSearchFriendsList.setOnItemClickListener(searchClick);
mView.findViewById(R.id.addfriends_img_AddFriend).setOnClickListener(this);
mView.findViewById(R.id.addfriends_img_Notes).setOnClickListener(this);
mView.findViewById(R.id.addfriends_img_Search).setOnClickListener(this);
mView.findViewById(R.id.Title_back).setOnClickListener(this);
((EditText)mView.findViewById(R.id.addfriends_search)).addTextChangedListener(this);
getIndexList();
displayIndex();

}

private void getIndexList() {
sideIndex = new LinkedHashMap<String, Integer>();
for (int i = 0; i < alphabaticalList.length; i++) {
if (sideIndex.get(i) == null)
sideIndex.put(alphabaticalList[i], i);
}
}

private void displayIndex() {
LinearLayout indexLayout = (LinearLayout) mView.findViewById(R.id.side_index);
List<String> indexList = new ArrayList<String>(sideIndex.keySet());
for (String index : indexList) {
mSiderowTextView = (TextView) getActivity().getLayoutInflater().inflate(R.layout.side_index_item, null);
mSiderowTextView.setText(index);
mSiderowTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mFriendsList.setSelection(sideIndex.get(mSiderowTextView.getText()));
}
});
indexLayout.addView(mSiderowTextView);
}
}

@Override
public void onRefresh() {
Logger.e("TAG", "onRefresh...AddFriendsFragment");
mView.findViewById(R.id.Title_Addfriend).setVisibility(View.GONE);
if(FragmentMainActivity.myPager.getCurrentItem() == 2){
((TextViewPlus)mView.findViewById(R.id.Title)).setText(getResources().getString(R.string.send_to));
}else {
((TextViewPlus)mView.findViewById(R.id.Title)).setText(getResources().getString(R.string.add_friend));
}
getFriendsList();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addfriends_img_AddFriend:
((ImageView)mView.findViewById(R.id.addfriends_img_AddFriend)).setImageResource(R.drawable.add_friends_hover);
((ImageView)mView.findViewById(R.id.addfriends_img_Notes)).setImageResource(R.drawable.notes_normal);
((ImageView)mView.findViewById(R.id.addfriends_img_Search)).setImageResource(R.drawable.searchtab_normal);
mView.findViewById(R.id.addfriends_friends_layout).setVisibility(View.VISIBLE);
mView.findViewById(R.id.addfriends_notes_layout).setVisibility(View.GONE);
mView.findViewById(R.id.addfriends_searchLayout).setVisibility(View.GONE);
mView.findViewById(R.id.addfriends_search).setVisibility(View.GONE);
getFriendsList();
break;
case R.id.addfriends_img_Notes:
((ImageView)mView.findViewById(R.id.addfriends_img_AddFriend)).setImageResource(R.drawable.add_friends_normal);
((ImageView)mView.findViewById(R.id.addfriends_img_Notes)).setImageResource(R.drawable.notes_hover);
((ImageView)mView.findViewById(R.id.addfriends_img_Search)).setImageResource(R.drawable.searchtab_normal);
mView.findViewById(R.id.addfriends_friends_layout).setVisibility(View.GONE);
mView.findViewById(R.id.addfriends_notes_layout).setVisibility(View.VISIBLE);
mView.findViewById(R.id.addfriends_searchLayout).setVisibility(View.GONE);
mView.findViewById(R.id.addfriends_search).setVisibility(View.GONE);
break;
case R.id.addfriends_img_Search:
((ImageView)mView.findViewById(R.id.addfriends_img_AddFriend)).setImageResource(R.drawable.add_friends_normal);
((ImageView)mView.findViewById(R.id.addfriends_img_Notes)).setImageResource(R.drawable.notes_normal);
((ImageView)mView.findViewById(R.id.addfriends_img_Search)).setImageResource(R.drawable.searchtab_hover);
mView.findViewById(R.id.addfriends_friends_layout).setVisibility(View.GONE);
mView.findViewById(R.id.addfriends_notes_layout).setVisibility(View.GONE);
mView.findViewById(R.id.addfriends_searchLayout).setVisibility(View.VISIBLE);
mView.findViewById(R.id.addfriends_search).setVisibility(View.VISIBLE);
break;
case R.id.Title_back:
FragmentMainActivity.myPager.setCurrentItem(2);
break;
}
}

public void getUserSearchList(){
if (!Utils.checkInternetConnection(getActivity())) {
Utils.showMessageDialog(getActivity(),getResources().getString(R.string.Alert),getResources().getString(R.string.connection));
} else {
UserSearchRequestTask userSearchRequestTask = new UserSearchRequestTask(getActivity());
userSearchRequestTask.setAsyncCallListener(new AsyncCallListener() {
@Override
public void onResponseReceived(Object response) {
mSearchData.clear();
all_f_model = (ManageFriendsModel) response;
if(all_f_model.userSearchData.size() > 0){
mSearchData.addAll(all_f_model.userSearchData);
mAdapter = new SearchListAdapter(getActivity(), mSearchData);
mSearchFriendsList.setAdapter(mAdapter);
}
}
@Override
public void onErrorReceived(String error) {}
});
userSearchRequestTask.execute(((EditText)mView.findViewById(R.id.addfriends_search)).getText().toString(),
Utils.sharedPreferences.getString(Utils.USER_ID, ""));
}
}

@Override
public void afterTextChanged(Editable arg0) {
/*if (0 != ((EditText)mView.findViewById(R.id.addfriends_search)).getText().length()) {
getUserSearchList();
} else {
mSearchData.clear();
mAdapter = new SearchListAdapter(getActivity(),mSearchData);
mSearchFriendsList.setAdapter(mAdapter);
}*/
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
if (((EditText)mView.findViewById(R.id.addfriends_search)).getText().length() != 0) {
getUserSearchList();
} else {
ArrayList<UserDataModel> temp = new ArrayList<UserDataModel>();
mAdapter = new SearchListAdapter(getActivity(),temp);
mSearchFriendsList.setAdapter(mAdapter);
}
}

public void getFriendsList() {
if (!Utils.checkInternetConnection(getActivity())) {
Utils.showMessageDialog(getActivity(),getResources().getString(R.string.Alert),getResources().getString(R.string.connection));
} else {
FriendsAddedMeRequestTask addedMeRequestTask = new FriendsAddedMeRequestTask(getActivity());
addedMeRequestTask.setAsyncCallListener(new AsyncCallListener() {
@Override
public void onResponseReceived(Object response) {
mAddFriendsList.clear();
all_f_model = (ManageFriendsModel) response;
if(all_f_model.friend_data.size() > 0){
try {
Gson gson = new Gson();
JSONObject jsonObject = new JSONObject(Utils.sharedPreferences.getString(Utils.FRIENDS_LIST,""));
ManageFriendsModel f_model = gson.fromJson(jsonObject.toString(), ManageFriendsModel.class);

for (int j = 0; j < all_f_model.friend_data.size(); j++) {
for (int i = 0; i < f_model.friend_data.size(); i++) {
if(Utils.validateString(all_f_model.friend_data.get(j).username)){
if(all_f_model.friend_data.get(j).username.equalsIgnoreCase(f_model.friend_data.get(i).username)){
all_f_model.friend_data.get(j).setF_selected(true);
}
}
}
mAddFriendsList.add(all_f_model.friend_data.get(j));
}
mFriendsListAdapter = new AddFriendsListAdapter(getActivity(), mAddFriendsList);
mFriendsList.setAdapter(mFriendsListAdapter);
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onErrorReceived(String error) {}
});
addedMeRequestTask.execute(Utils.sharedPreferences.getString(Utils.USER_ID, ""));
}
}

OnItemClickListener searchClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, final int position, long arg3) {
if (mSearchData.get(position).already_friend.equalsIgnoreCase("1") || mSearchData.get(position).isSelected()) {
mSearchData.get(position).setSelected(false);
String mFriendId = mSearchData.get(position).id;
if (!Utils.checkInternetConnection(getActivity())) {
Utils.showMessageDialog(getActivity(),getResources().getString(R.string.Alert),getResources().getString(R.string.connection));
} else {
GetFriendsListRequestTask getFriendsListRequestTask = new GetFriendsListRequestTask(getActivity());
getFriendsListRequestTask.setAsyncCallListener(new AsyncCallListener() {
@Override
public void onResponseReceived(Object response) {
all_f_model = (ManageFriendsModel) response;
if(all_f_model.status.equalsIgnoreCase("0")){
mSearchData.get(position).setSelected(false);
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onErrorReceived(String error) {}
});
getFriendsListRequestTask.execute("remove", Utils.sharedPreferences.getString(Utils.USER_ID, ""), mFriendId, "0");
}
}
else{
mSearchData.get(position).setSelected(true);
String mFriendId = mSearchData.get(position).id;
if (!Utils.checkInternetConnection(getActivity())) {
Utils.showMessageDialog(getActivity(),getResources().getString(R.string.Alert),getResources().getString(R.string.connection));
} else {
GetFriendsListRequestTask getFriendsListRequestTask = new GetFriendsListRequestTask(getActivity());
getFriendsListRequestTask.setAsyncCallListener(new AsyncCallListener() {
@Override
public void onResponseReceived(Object response) {
all_f_model = (ManageFriendsModel) response;
if(all_f_model.status.equalsIgnoreCase("2")){
Utils.showMessageDialog(getActivity(), getActivity().getResources().getString(R.string.Alert),
"Already a friend");
mSearchData.get(position).setSelected(true);
mAdapter.notifyDataSetChanged();
}else{
mSearchData.get(position).setSelected(true);
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onErrorReceived(String error) {}
});
getFriendsListRequestTask.execute("insert", Utils.sharedPreferences.getString(Utils.USER_ID, ""), mFriendId, "0");
}
}
mAdapter.notifyDataSetChanged();
}
};

OnItemClickListener friendClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// friends list
if (mAddFriendsList.get(position).isF_selected()) {
mAddFriendsList.get(position).setF_selected(false);
String mFriendId = mAddFriendsList.get(position).user_id;
if (!Utils.checkInternetConnection(getActivity())) {
Utils.showMessageDialog(getActivity(),getResources().getString(R.string.Alert),getResources().getString(R.string.connection));
} else {
GetFriendsListRequestTask getFriendsListRequestTask = new GetFriendsListRequestTask(getActivity());
getFriendsListRequestTask.execute("remove", Utils.sharedPreferences.getString(Utils.USER_ID, ""), mFriendId, "0");
}
}
else{
mAddFriendsList.get(position).setF_selected(true);
String mFriendId = mAddFriendsList.get(position).user_id;
if (!Utils.checkInternetConnection(getActivity())) {
Utils.showMessageDialog(getActivity(),getResources().getString(R.string.Alert),getResources().getString(R.string.connection));
} else {
GetFriendsListRequestTask getFriendsListRequestTask = new GetFriendsListRequestTask(getActivity());
getFriendsListRequestTask.execute("insert", Utils.sharedPreferences.getString(Utils.USER_ID, ""), mFriendId, "0");
}
}
mFriendsListAdapter.notifyDataSetChanged();
}
};
}

2) SearchListAdapter.java
===================
package com.boss.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.boss.R;
import com.boss.model.UserDataModel;
import com.boss.utils.Utils;

public class SearchListAdapter extends BaseAdapter {

private Context mContext;
private LayoutInflater infalter;
private ArrayList<UserDataModel> data;
public SearchListAdapter(Context mContext, ArrayList<UserDataModel> data) {
super();
this.mContext = mContext;
this.data = data;
}


@Override
public int getCount() {
return data.size();
}

@Override
public Object getItem(int position) {
return data.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
View vi = convertView;
if(convertView == null){
infalter = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
vi = infalter.inflate(R.layout.search_row, null);

if(Utils.validateString(data.get(position).user_name)){
((TextView)vi.findViewById(R.id.search_row_txt_name)).setText(data.get(position).user_name);
}
if(data.get(position).already_friend.equalsIgnoreCase("1") || data.get(position).isSelected()){
((ImageView)vi.findViewById(R.id.search_row_btn_addfriend)).setImageResource(R.drawable.checkbox_hover);
}else if(!data.get(position).isSelected()){
((ImageView)vi.findViewById(R.id.search_row_btn_addfriend)).setImageResource(android.R.drawable.ic_menu_add);
}
return vi;
}
}

3) search_row
===========
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.boss"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/send_details_row_bg"
    android:orientation="horizontal"
    android:padding="@dimen/activity_padding" >

    <ImageView
        android:id="@+id/search_row_imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/send_details_icon" />

    <com.boss.TextViewPlus
        android:id="@+id/search_row_txt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toLeftOf="@+id/search_row_btn_addfriend"
        android:layout_toRightOf="@+id/search_row_imageView1"
        android:padding="5dp"
        android:textColor="@color/gray"
        android:textSize="@dimen/Logintitle_text2"
        app:customFont="SinkinSans-300Light_0.otf" />

    <ImageView
        android:id="@+id/search_row_btn_addfriend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:src="@android:drawable/ic_menu_add" />

</RelativeLayout>

4) fragment_add_friend
=================
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/mainbg2" >

    <include
        android:id="@+id/addfriends_titlebar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/titlebar" />

    <LinearLayout
        android:id="@+id/addfriends_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/addfriends_titlebar" >

        <ImageView
            android:id="@+id/addfriends_img_AddFriend"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="fitXY"
            android:src="@drawable/add_friends_hover" />

        <ImageView
            android:id="@+id/addfriends_img_Notes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="fitXY"
            android:src="@drawable/notes_normal" />

        <ImageView
            android:id="@+id/addfriends_img_Search"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="fitXY"
            android:src="@drawable/searchtab_normal" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/addfriends_friends_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/addfriends_layout" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Friends who added me"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <ListView
                android:id="@+id/addfriends_listview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:cacheColorHint="@android:color/transparent"
                android:listSelector="@android:color/transparent"
                android:overScrollMode="never"
                android:scrollbars="none"
                android:visibility="visible" >
            </ListView>
        </LinearLayout>

        <ScrollView
            android:id="@+id/sendto_side_index"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:overScrollMode="never"
            android:scrollbars="none"
            android:visibility="gone" >

            <LinearLayout
                android:id="@+id/side_index"
                android:layout_width="40dip"
                android:layout_height="fill_parent"
                android:gravity="center_horizontal"
                android:orientation="vertical" >
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>

    <ScrollView
        android:id="@+id/addfriends_notes_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/addfriends_layout"
        android:overScrollMode="never"
        android:scrollbars="none"
        android:visibility="gone" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingLeft="@dimen/camera_padding"
            android:paddingRight="@dimen/camera_padding"
            android:paddingTop="@dimen/activity_padding" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="@dimen/activity_padding"
                android:text="@string/notes_text" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/add_friends_popupbg"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="@drawable/add_friends_popup_tital" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="@drawable/add_friends_popup_settings_normal" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="@drawable/add_friends_popup_privacy_normal" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="@drawable/add_friends_popup_contacts_normal" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="@drawable/add_friends_popup_boss_normal" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="@drawable/add_friends_popup_favourites_normal" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/addfriends_searchLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/addfriends_layout"
        android:orientation="vertical"
        android:visibility="gone" >

        <EditText
            android:id="@+id/addfriends_search"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/Login_margin"
            android:layout_marginLeft="@dimen/activity_padding"
            android:layout_marginRight="@dimen/activity_padding"
            android:layout_marginTop="@dimen/Login_margin"
            android:background="@drawable/search1"
            android:ems="10"
            android:hint="Search"
            android:inputType="textPersonName"
            android:paddingLeft="40dp" >
        </EditText>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@color/light_gray" />

        <ListView
            android:id="@+id/addfriends_search_listview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="@android:color/transparent"
            android:listSelector="@android:color/transparent"
            android:overScrollMode="never"
            android:scrollbars="none" >
        </ListView>
    </LinearLayout>

</RelativeLayout>

No comments:

Post a Comment